22

WebView をロードするアプリケーションを作成しました。ログインするには、Web サイトで基本認証が必要です。デフォルトのブラウザーで Web サイトにアクセスしようとすると、ユーザー名とパスワードの入力を求めるポップアップ ボックスが表示されます。

アプリケーションから Web サイトにアクセスしようとすると、エラー 401 が表示され、ポップアップが表示されません。誰かが私を助けてくれるかどうか疑問に思っていましたか?

4

6 に答える 6

37

パトリックが説明するよりも洗練された解決策は、onReceivedHttpAuthRequestここで説明されているWebViewClientのメソッドを使用することだと思います:http://www.mail-archive.com/android-developers@googlegroups.com/msg30468.html

@Override
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
    handler.proceed("username", "password");
}
于 2012-01-21T13:59:01.547 に答える
13

Web サイトには認証が必要です。

まず、エラーに反応します。

webview.setWebViewClient(new WebViewClient() {
   public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
     if (errorCode == 401) {
         // show alert to enter username and password
         // then when those are entered in the alert,
         //     set it through the setHttpAuthUsernamePassword(...) shown below
         //     and then reload the site
     }
   }
 });

この関数を使用して、ユーザーとパスワードを設定します。WebView.setHttpAuthUsernamePassword()

webview.setHttpAuthUsernamePassword(host, realm, username, password);

すべて文字列です。ホストとレルムの意味の詳細については、上記のリンクを参照してください。

ここにある解決策: WebView にいくつかの基本的な認証資格情報を提供しますか?

于 2010-11-19T00:21:54.767 に答える
1

これは私のために働いた

webView.setWebViewClient(new MyWebViewClient ());

//add this class in main activity 
class MyWebViewClient extends WebViewClient {
    @Override
    public void onReceivedHttpAuthRequest(WebView view,
                                          HttpAuthHandler handler, String host, String realm) {

        handler.proceed("your username", "your password");

    }
}
于 2021-05-27T13:01:23.630 に答える