我们如何处理HTTP错误,例如:android.webview中的401,403,404,500?

时间:2014-09-30 09:50:16

标签: android android-webview

Android.webview.WebViewClient无法加载URL时,

WebViewClient.onReceivedError类允许活动收到WebView回调,但HTTP错误没有回调,例如:401,403,404, 500. 我们如何处理WebView中的错误?

以下是一些代码:

public class WebViewActivity extends Activity {
WebView webView;
public String TAG="log";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
        webView = (WebView) findViewById(R.id.webview1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://192.168.0.6:8081/karbonn/add/apk//3_1/");


    //  webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient(){
    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
          String pageTitle = view.getTitle();
           Toast.makeText(getBaseContext(), pageTitle, Toast.LENGTH_SHORT).show();
         // view.get
         /*  if (pageTitle.contains("Apache Tomcat/7.0.40 - Error report")) {
              // view.goBack();
               WebViewActivity.this.finish();
            //view.loadUrl("https://www.google.co.in/");
        }*/

    }

    @Override
    public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
        //Log.e(Tag, "onReceivedError");
        //int i=errorCode;
        if (errorCode == ERROR_HOST_LOOKUP || errorCode == ERROR_FILE_NOT_FOUND) {
            // TODO your dialog here
            Log.e("error", "ERROR_FILE_NOT_FOUND"+errorCode);
        }
        Log.e("error", "onReceivedError"+errorCode);
        if (errorCode==-10) {
             WebViewActivity.this.finish();
        }


                    String errormsg=description;
            String url=failingUrl;

        // TODO Auto-generated method stub
         Log.e("error code:" ,""+errorCode);
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view,
            String url) {
        // TODO Auto-generated method stub
         Log.e("WebResourceResponse" ,""+url);
        return super.shouldInterceptRequest(view, url);
    }

    @Override
    public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
        // TODO Auto-generated method stub
        return super.shouldOverrideKeyEvent(view, event);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        String myurl=url;
        Log.e("url", ""+myurl);
        return super.shouldOverrideUrlLoading(view, url);
    }
});
        //webView.loadUrl("http://trax.claym.in/click.php?c=209&key=c73n783g2tm2377yd73d6n5m");
    //webView.loadUrl("https://www.google.co.in/");
        //animation url
//  webView.loadUrl("http://www.dogwonder.co.uk/wp-content/uploads/2009/12/tumblr_ku2pvuJkJG1qz9qooo1_r1_400.gif");
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
     // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
        webView.goBack();
        return true;
    }
    // TODO Auto-generated method stub
    return super.onKeyDown(keyCode, event);
}

}

1 个答案:

答案 0 :(得分:2)

抱歉,但你不能。

你有开发人员告诉的错误和线程。

https://code.google.com/p/android/issues/detail?id=968

您可以使用新方法

在新的Android M(API 23)上进行操作
public void onReceivedHttpError (WebView view, WebResourceRequest request, WebResourceResponse errorResponse)

参考:https://developer.android.com/intl/es/reference/android/webkit/WebViewClient.html#onReceivedHttpError%28android.webkit.WebView,%20android.webkit.WebResourceRequest,%20android.webkit.WebResourceResponse%29

希望这有帮助