search

Home  >  Q&A  >  body text

How to judge 404 in webview under android?

I am using

 @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            PtrCLog.d("WebFragment", "onReceivedError: " + "");

        }
        
        

This method has not been rolled back. After checking, it is said that API23 is required. . Does anyone know of any other ways to get 404?

phpcn_u1582phpcn_u15822797 days ago645

reply all(2)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-16 13:28:53

            new WebViewClient() {
                @Override
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    super.onReceivedError(view, errorCode, description, failingUrl);
                    if (errorCode == 404) {
                        doSomething();
                    }
                }
    
                @Override
                public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                    super.onReceivedError(view, request, error);
                    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                        int errorCode = error.getErrorCode();
                        if (errorCode == 404) {
                            doSomething();
                        }
                    }
                }
            };

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-16 13:28:53

    Run an AsyncTask in onPageStarted, use an Http client such as OkHttpClient in the AsyncTask to make a request for the URL that needs to be accessed, and obtain the code

        class WebViewStatusRequester extends AsyncTask<String, String, Integer> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                web.setVisibility(View.GONE);
            }
            @Override
            protected void onPostExecute(Integer result) {
                super.onPostExecute(result);
                if(result == 1) {
                    web.setVisibility(View.VISIBLE);
                } else if(result == 0) {
                    showLoadFail();
                }
            }
            @Override
            protected Integer doInBackground(String... params) {
                String url = params[0];
                if(url.substring(0, 4).equals("file") == false) {
                    try {
                        OkHttpClient client = new OkHttpClient();
                        Request request = new Request.Builder().url(url).build();
                        Response response = client.newCall(request).execute();
                        if(response.isSuccessful()) {
                            return 1;
                        } else {
                            Log.i("TAG", "fail code:" + response.code());
                            return 0;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return 0;
                }
                return 1;
            }
        }

    Here, Webview and OkHttpClient are actually loaded, but the WebView is hidden when the AsyncTask is running. If it is confirmed to be 200, it will be displayed. If it is not 200, the page that failed to load will be displayed.

    reply
    0
  • Cancelreply