首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于webview不工作的android BasicAuthentication

用于webview不工作的android BasicAuthentication
EN

Stack Overflow用户
提问于 2014-01-17 11:56:50
回答 4查看 16.4K关注 0票数 12

嗨,我想从我的安卓网页上打开这个url http://3864.cloud-matic.net/,我尝试过很多种方法,但是这个应用程序甚至没有打开mainActivity。下面是我尝试过的。

代码语言:javascript
复制
 public void onCreate(Bundle state) {
    super.onCreate(state);
    setContentView(R.layout.main);
    WebView webView = (WebView) findViewById(R.id.webView1);
    webView.setHttpAuthUsernamePassword("cloud-matic.net/",     "realm", username, password);
    webView.loadUrl("http://3864.cloud-matic.net/");
}

请告诉我我哪里错了。阿里

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-01-17 13:50:10

代码语言:javascript
复制
webview.setWebViewClient(new MyWebViewClient ());

private class MyWebViewClient extends WebViewClient {
    @Override
    public void onReceivedHttpAuthRequest(WebView view,
            HttpAuthHandler handler, String host, String realm) {
        handler.proceed("me@test.com", "mypassword");
    }
}

这是投票最多的解决方案,我不知道在哪里设置open.Please建议的网址。

票数 26
EN

Stack Overflow用户

发布于 2020-08-06 01:19:06

我不确定这是否会对某人有所帮助,但我用用户名和密码实现了onReceivedHttpAuthRequest功能(就像上面的人一样),除了在Android上,它在每个设备上都正常工作。然后,我意识到奥利奥手机收到的http请求不是来自主框架,它给了我401错误。

在我检查的onReceivedHttpError函数中,如果http错误来自主框架,则为:

代码语言:javascript
复制
         `override fun onReceivedHttpError(
                view: WebView?,
                request: WebResourceRequest?,
                errorResponse: WebResourceResponse?
            ) {
                super.onReceivedHttpError(view, request, errorResponse)
                if(request.isForMainFrame){ //do smt. with the error}
               }`

然后它开始正常工作。

票数 2
EN

Stack Overflow用户

发布于 2016-04-13 10:32:48

这应该是完美的代码片段。

代码语言:javascript
复制
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setFocusable(true);
    webView.loadUrl("http://3864.cloud-matic.net/");
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onReceivedHttpAuthRequest(WebView view,
                                              HttpAuthHandler handler, String host, String realm) {
            handler.proceed("me@test.com", "mypassword");
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            prDialog.setMessage("Please wait ...");
            prDialog.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (prDialog != null) {
                prDialog.dismiss();
            }
        }
    });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21185400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档