我有一个android WebView,按照其他线程的建议,设置了一个WebViewClient,用于通过使用onHttpErrorReceived来覆盖500个来自Android6及更高版本的HTTP错误,如下所示:
webview.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.d("FREEBROWSER", "----------------- ERROR deprecated ---------------");
}
@Override
@TargetApi(android.os.Build.VERSION_CODES.M)
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
Log.d("FREEBROWSER", "----------------- ERROR " + rerr.getDescription() + " ---------------");
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
}
@Override
@TargetApi(android.os.Build.VERSION_CODES.M)
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
onReceivedError(view, errorResponse.getStatusCode(), errorResponse.getReasonPhrase(), request.getUrl().toString());
}
});这对于运行API级别23及更高版本的设备来说非常好(正如这里的docs和其他线程所提到的那样),但是如果您也希望捕获API级别23以下的设备的500错误,这当然是不令人满意的。
我的Android4.4.2设备完全没有错误回调。两个版本都没有
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 也不是那个版本
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr)正在调用500个错误。
通过查看低于API 23的设备的可能解决方案,我基本上在许多变体中找到了2种解决方案(我不想在这里再次解释它们):
而且,据我所知,没有计划发布此回调的supportlib版本,以便在旧版本中使用。
我的问题是:在运行安卓版本的设备低于APILevel23的情况下,是否有其他干净的方法支持捕获HTTP状态代码?是否有人为客户实现了合适的解决方案,最多不包括附加请求或JavaScript注入?
发布于 2017-04-04 18:38:11
您可以尝试下载HTML字符串,并使用此初始请求检查响应头。例如,使用HttpURLConnection (或OkHttpClient)来实现这一点。在那里,您可以使用HttpURLConnection.getResponseCode()检查响应。如果响应代码对您有好处,请使用WebView.loadDataWithBaseURL()加载下载的字符串,请参阅https://developer.android.com/reference/android/webkit/WebView.html#loadDataWithBaseURL
https://stackoverflow.com/questions/43207466
复制相似问题