Status Codes in Android Studio WebView

Asked 2 years ago, Updated 2 years ago, 48 views

In order to get the status code from Android Studio in Webview, I looked at the reference site and wrote the code, but the app fell off in the middle.How can I prevent the app from falling off?

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

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/");
    }
}

java

2022-09-29 21:47

1 Answers

Excerpt only the relevant parts.

byte[]data=null;
try{
    if(HttpStatus.SC_OK==res.getStatusLine().getStatusCode()){
        data=EntityUtils.toByteArray(entity);
    }
}
InputStream stream=new ByteArrayInputStream(data);

Therefore, except for statusCode200, data==null.
There is no error log, so I can't determine if it's caused by Coco, but I think it will be helpful.


2022-09-29 21:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.