WebView をロードするアプリケーションを作成しました。ログインするには、Web サイトで基本認証が必要です。デフォルトのブラウザーで Web サイトにアクセスしようとすると、ユーザー名とパスワードの入力を求めるポップアップ ボックスが表示されます。
アプリケーションから Web サイトにアクセスしようとすると、エラー 401 が表示され、ポップアップが表示されません。誰かが私を助けてくれるかどうか疑問に思っていましたか?
WebView をロードするアプリケーションを作成しました。ログインするには、Web サイトで基本認証が必要です。デフォルトのブラウザーで Web サイトにアクセスしようとすると、ユーザー名とパスワードの入力を求めるポップアップ ボックスが表示されます。
アプリケーションから Web サイトにアクセスしようとすると、エラー 401 が表示され、ポップアップが表示されません。誰かが私を助けてくれるかどうか疑問に思っていましたか?
パトリックが説明するよりも洗練された解決策は、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");
}
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 にいくつかの基本的な認証資格情報を提供しますか?
これは私のために働いた
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");
}
}