14

こんにちは
リストビューには、サーバーから画像ファイルをロードする必要があるWebビューがあります。画像が存在しない場合、ダミー画像が必要です。試してみました

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にダミー画像を含むこのWebビューがあります.onPageFinishedリスナーは、すべての画像URLが読み込まれた後に呼び出されますが、404エラーを生成するURLに対してonReceivedErrorは呼び出されません。

4

6 に答える 6

8

WebViewClient.onReceivedError() の代わりに WebViewClient.onReceivedHttpError() をオーバーライドする必要がありました。

    @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) {
}
于 2016-07-20T21:37:43.063 に答える
1

@Neerajは正しい方向に進んでいますが、私のアプリではWebビューを更新できるため、新しい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();
      }
于 2012-03-14T19:26:25.173 に答える
-1

そのコードは正しいようです。ページが 404 エラーを生成していない可能性はありますか?

于 2011-03-25T14:53:12.923 に答える
-1
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 ); 
    } 
  }); 
于 2012-01-06T15:30:17.967 に答える