2016-11-11 18 views
0

私はラズベリーパイからウェブページを読み込むAndroidアプリにWebViewを持っています。このウェブページはBasic Authentificationによって保護されています。Android WebView HTTP認証の再試行を中止します

webView.setWebViewClient(new WebViewClient() { 
    @Override 
    public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) { 
     handler.proceed(user, pass); 
    } 

    @Override 
    public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) { 
     super.onReceivedHttpError(view, request, errorResponse); 
// I'm always landing here if authentification fails. 
    } 
}); 

webView.loadUrl(protocol + "://" + hostname + ":" + port); 

認証が失敗した場合、どのように再試行を停止できますか?私はこれを停止しない場合、アプリは、私がアプリケーションを閉じるまで毎秒要求を送信します。

ありがとうございました。

答えて

0
webView.setWebViewClient(new WebViewClient() { 
     int tries = 0; 

     @Override 
     public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) { 
      handler.proceed(user, pass); 
     } 

     @Override 
     public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) { 
      super.onReceivedHttpError(view, request, errorResponse); 

      if (tries > 1) { 
       webView.destroy(); 
       Toast.makeText(getApplicationContext(), errorResponse.getStatusCode() + ": " 
         + errorResponse.getReasonPhrase(), Toast.LENGTH_LONG).show(); 
      } 
      tries++; 
     } 
    }); 

    webView.loadUrl(protocol + "://" + hostname + ":" + port); 
関連する問題