suchen

Heim  >  Fragen und Antworten  >  Hauptteil

Wie beurteilt man 404 in der Webansicht unter Android?

Ich verwende

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

        }
        
        

Diese Methode wurde nicht zurückgesetzt. Ich habe nachgesehen und festgestellt, dass sie API23 erfordert. . Kennt jemand andere Möglichkeiten, an 404 zu kommen?

phpcn_u1582phpcn_u15822743 Tage vor612

Antworte allen(2)Ich werde antworten

  • 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();
                        }
                    }
                }
            };

    Antwort
    0
  • 滿天的星座

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

    在onPageStarted里边跑一个AsyncTask,在AsyncTask里用OkHttpClient之类的Http客户端对需要访问的URL进行一次请求,取得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;
            }
        }

    在这里实际上是Webview和OkHttpClient进行了加载,只是AsyncTask跑的时候把WebView隐藏起来了,确认是200了就显示,不是200就显示加载失败的页面

    Antwort
    0
  • StornierenAntwort