2013-07-04 51 views
0

我有一個webView其中url是:https://uesr:[email protected]:443/test-mobile當我想要加載此頁面時,它是不可避免的,我從頁面中刪除身份驗證並刪除user:user @ page work。如何將身份驗證添加到webview以使用該網址?在webview中錯誤的顯示頁面在android

這是我的web視圖:下面的代碼

formWebView.setWebViewClient(new WebViewClient() { 
      @Override 
      public void onReceivedHttpAuthRequest(WebView view, 
               HttpAuthHandler handler, 
               String host, 
               String realm){ 
      handler.proceed(loggedUser.getLogin(), loggedUser.getPass()); 
      } 

      public void onReceivedSslError(WebView view, 
             SslErrorHandler handler, 
             SslError error) { 
      handler.proceed() ; 
      } 
     }); 
+0

看起來像HTTP基本認證。看看這是否有幫助:http://stackoverflow.com/questions/8935537/android-webview-with-https-connection-and-basic-auth-how-to-get-this-working –

+0

字符串usernameRandomPassword =「httpwatch:」 + UUID.randomUUID()。toString(); \t String authorization =「Basic」+ Base64.encodeToString(usernameRandomPassword.getBytes(「UTF-8」),Base64.NO_WRAP); 我必須在哪裏設置我的登錄名和密碼? – edi233

+0

字符串usernameRandomPassword =「user:user」; –

回答

3

我的解決我的問題。

webView.setWebViewClient(new MyWebViewClient());

private class MyWebViewClient extends WebViewClient 
{ 

    public void onReceivedHttpAuthRequest(WebView view, 
      HttpAuthHandler handler, String host, String realm) { 

     handler.proceed("UserName", "Password"); 
    } 

    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
     if (dialog != null) { 
      if (!dialog.isShowing()) { 
       dialog.show(); 
      } 
     } 
    } 

    public void onLoadResource(WebView view, String url) { 
     // TODO Auto-generated method stub 
     super.onLoadResource(view, url); 

    } 

    public void onScaleChanged(WebView view, float oldScale, float newScale) { 
     // TODO Auto-generated method stub 
     super.onScaleChanged(view, oldScale, newScale); 
    } 

    public void onTooManyRedirects(WebView view, Message cancelMsg, 
      Message continueMsg) { 
     // TODO Auto-generated method stub 
     super.onTooManyRedirects(view, cancelMsg, continueMsg); 
    } 

    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     // TODO Auto-generated method stub 




     return super.shouldOverrideUrlLoading(view, url); 

    } 

    public void doUpdateVisitedHistory(WebView view, String url, 
      boolean isReload) { 
     // TODO Auto-generated method stub 
     super.doUpdateVisitedHistory(view, url, isReload); 

    } 

    public void onReceivedError(WebView view, int errorCode, 
      String description, String failingUrl) { 
     // TODO Auto-generated method stub 
     super.onReceivedError(view, errorCode, description, failingUrl); 
    } 

    public void onPageFinished(WebView view, String url) { 
     if (dialog != null) { 

      dialog.dismiss(); 
     } 
    } 

} 
+0

完美!你救了我... –

0

寫在你的onCreate方法:

formWebView.getSettings().setJavaScriptEnabled(true); 
formWebView.loadUrl(url); 
formWebView.getSettings().setBuiltInZoomControls(true);