在webview中生成错误404 - 不重定向到所需页面

时间:2017-05-14 04:00:09

标签: android webview

下面的代码在生成错误404时没有重定向到指定的链接,而是显示错误页面。请看下面的代码。

@Override
public void onReceivedError(WebView view, int errorCode,
                            String description, String failingUrl) {
    //Log.e(TAG," Error occured while loading the page at Url"+ failingUrl+"." +description);

    if (errorCode == 404) {
        // show Alert here for Page Not found
        view.loadUrl("http://google.com");
    } else {
        Intent intent = new Intent(MainActivity.this, noconnection.class);
        intent.putExtra("a", "mainactivity is source");
        startActivity(intent);

        Toast.makeText(getApplicationContext(), "Error occured, please check network connectivity", Toast.LENGTH_SHORT).show();
        super.onReceivedError(view, errorCode, description, failingUrl);
    }
}
  

错误日志:

05-14 09:54:36.655 5673-5673/com.hare.pat E/SysUtils: ApplicationContext is null in ApplicationStatus
05-14 09:54:36.885 5673-5673/com.hare.pat E/chromium: [ERROR:browser_gpu_channel_host_factory.cc(258)] Failed to init browser shader disk cache.
05-14 09:54:37.026 5673-5673/com.hare.pat E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)

1 个答案:

答案 0 :(得分:0)

errorcode对应于ERROR_ *常量值,而不是404。

你应该使用ERROR_FILE_NOT_FOUND(其常数值为-14)(documentation

您可以浏览更多常量列表here 我在这里添加了误差常数以供参考。

/** Generic error */
public static final int ERROR_UNKNOWN = -1;
/** Server or proxy hostname lookup failed */
public static final int ERROR_HOST_LOOKUP = -2;
/** Unsupported authentication scheme (not basic or digest) */
public static final int ERROR_UNSUPPORTED_AUTH_SCHEME = -3;
/** User authentication failed on server */
public static final int ERROR_AUTHENTICATION = -4;
/** User authentication failed on proxy */
public static final int ERROR_PROXY_AUTHENTICATION = -5;
/** Failed to connect to the server */
public static final int ERROR_CONNECT = -6;
/** Failed to read or write to the server */
public static final int ERROR_IO = -7;
/** Connection timed out */
public static final int ERROR_TIMEOUT = -8;
/** Too many redirects */
public static final int ERROR_REDIRECT_LOOP = -9;
/** Unsupported URI scheme */
public static final int ERROR_UNSUPPORTED_SCHEME = -10;
/** Failed to perform SSL handshake */
public static final int ERROR_FAILED_SSL_HANDSHAKE = -11;
/** Malformed URL */
public static final int ERROR_BAD_URL = -12;
/** Generic file error */
public static final int ERROR_FILE = -13;
/** File not found */
public static final int ERROR_FILE_NOT_FOUND = -14;
/** Too many requests during this load */
public static final int ERROR_TOO_MANY_REQUESTS = -15;