Display characters in Android Studio TextView

Asked 2 years ago, Updated 2 years ago, 50 views

http://saki0n.blogspot.jp/2013/04/android-webview.html

I'm trying to get a status code (such as 404 or 500) on AndroidStudio by referring to the above site, but I think HttpStatus.SC_OK is the status code part and the res.getStatusLine().getStatusCode() part is the number 200. Can I display this in ?


in the if statement textView.setText(res.getStatusLine().getStatusCode(); code will cause the app to drop.

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

public class MainActivity extensions AppCompatActivity{

    private TextView textView;
    @ Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView myWebView= (WebView) findViewById (R.id.webView);
        textView=(TextView) findViewById (R.id.textView);
        // myWebView.setWebViewClient(newWebViewClient());
        myWebView.setWebViewClient(newWebViewClient(){
            @TargetApi (Build.VERSION_CODES.HONEYCOMB)
            @ Override
            public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
                if(!url.matches("https?://[\\w\\.\\-]+(/.*)?")) {
                    return super.shouldInterceptRequest(view, url);
                }
                HttpGet req = new HttpGet(url);
                DefaultHttpClient client=new DefaultHttpClient();
                String mimeType=null, encoding=null;
                byte [ ] data = null;
                try{
                    HttpResponse res=client.execute(req);
                    // You can get the status code here!
                    if(HttpStatus.SC_OK==res.getStatusLine().getStatusCode()){
                        HttpEntity entity = res.getEntity();
                        Header mimeHeader=entity.getContentType();
                        textView.setText(res.getStatusLine().getStatusCode());
                        if(null!=mimeHeader)mimeType=mimeHeader.getValue();
                        Header encodingHeader=entity.getContentEncoding();
                        if(null!=encodingHeader)encoding=encodingHeader.getValue();
                        data=EntityUtils.toByteArray(entity);
                    }
                } catch(Exceptione){
                    String msg = e.getMessage();
                    Log.e(this.getClass().getSimpleName(),(null!=msg)?msg:"");
                } US>finally
                    req.abort();
                    client.getConnectionManager().shutdown();
                }
                InputStream stream=new ByteArrayInputStream(data);
                return new WebResourceResponse(mimeType, encoding, stream);
            }
        });
        myWebView.loadUrl("https://www.google.com/");
    }
}

android

2022-09-30 19:37

1 Answers

res.getStatusLine().getStatusCode() returns a value of type int.
If you pass the int type to textView.setText(), it will be determined to be the name of strings.xml, so it should be an error.

int code=res.getStatusLine().getStatusCode();
textView.setText("+code);

If you convert the int value into a string by combining it with an empty string, you will see it correctly.


2022-09-30 19:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.