gpt4 book ai didi

Android WebViewClient onReceivedError 没有调用 404 错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:10:16 42 4
gpt4 key购买 nike


在 ListView 中,我有一个 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。任何猜测如何去做。

最佳答案

我必须重写 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 &gt;= 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) {
}

关于Android WebViewClient onReceivedError 没有调用 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5433818/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com