喜
在列表视图中我有一个webview应该从服务器加载图像文件,当没有图像存在时我需要一个虚拟图像。我试过
holder.image.setWebViewClient(new WebViewClient()
{
@Override
public void onReceivedError( WebView view, int errorCode, String description, String failingUrl)
{
System.out.println("description error" + description);
view.setVisibility( View.GONE );
}
@Override
public void onPageFinished(WebView view, String url) {
view.setVisibility( View.VISIBLE );
}
});
我在FrameLayout中有一个带虚拟图像的webview,在加载每个图像url后调用onPageFinished监听器,但是没有为产生404错误的url调用onReceivedError。任何猜测该怎么做。
答案 0 :(得分:4)
我必须覆盖WebViewClient.onReceivedHttpError()而不是WebViewClient.onReceivedError()。
@Override
public void onReceivedHttpError(final WebView view, final WebResourceRequest request, WebResourceResponse errorResponse) {
final int statusCode;
// SDK < 21 does not provide statusCode
if (Build.VERSION.SDK_INT < 21) {
statusCode = STATUS_CODE_UNKNOWN;
} else {
statusCode = errorResponse.getStatusCode();
}
Log().d(LOG_TAG, "[onReceivedHttpError]" + statusCode);
}
来自WebClient文档:
/**
* Notify the host application that an HTTP error has been received from the server while
* loading a resource. HTTP errors have status codes >= 400. This callback will be called
* for any resource (iframe, image, etc), not just for the main page. Thus, it is recommended to
* perform minimum required work in this callback. Note that the content of the server
* response may not be provided within the <b>errorResponse</b> parameter.
* @param view The WebView that is initiating the callback.
* @param request The originating request.
* @param errorResponse Information about the error occured.
*/
public void onReceivedHttpError(
WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
}
答案 1 :(得分:3)
我今天遇到了同样的问题,
问题:总是调用onPageFinished。如果有错误,将在onErrorReceived之后调用。
这是我找到的解决方案:
holder.image.setWebViewClient(new WebViewClient() {
private boolean error;
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
error = false;
}
@Override
public void onReceivedError( WebView view, int errorCode, String description, String failingUrl) {
error = true;
System.out.println("description error" + description);
view.setVisibility( View.GONE );
}
@Override
public void onPageFinished(WebView view, String url) {
if (!error) {
view.setVisibility( View.VISIBLE );
}
error = false;
}
});
答案 2 :(得分:0)
该代码看起来正确;您的网页可能没有生成404错误吗?
答案 3 :(得分:0)
holder.image.setWebViewClient(new WebViewClient() {
boolean bReceivedError = false;
@Override
public void onReceivedError( WebView view, int errorCode,
String description, String failingUrl) {
bReceivedError = true;
view.setVisibility( View.GONE );
}
@Override
public void onPageFinished(WebView view, String url) {
if(!bReceivedError)
view.setVisibility( View.VISIBLE );
}
});
答案 4 :(得分:0)
@Neeraj在正确的轨道上,但我的应用程序允许刷新webview,所以我需要在任何新的URL加载之前清除错误状态。此外,错误标志必须存储为父活动的数据成员,以便在onPageStart()和onPageFinish()期间保持不变 - 这些方法可以在 onError()之后调用。
public class MyActivity extends Activity {
private boolean isError;
...
protected void onResume() {
super.onResume();
isError = false;
myWebView.loadUrl(myUrl);
}
public class MyWebViewClient extends WebViewClient {
/**
* can be called even after error (embedded images?), so error flag must keep state as data-member in activity, cleared by activity before each loadUrl();
*/
@Override
public void onPageFinished(WebView view, String url) {
if (!isError)
showContent();
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
isError = true;
showError();
}