webview

时间:2015-06-05 11:37:08

标签: android webview authorization android-authenticator

我试图在成功登录后访问网页,在那里我获取用户名和密码(来自我的应用程序中的EditTexts“user =(EditText)findViewById(R.id.user);”和“pass =( EditText)findViewById(R.id.pass);“)并将其发布到服务器。

问题是webview仅在具有android 4.2.2但在4.4.2,4.4.4和5.0的手机/平板电脑上显示文本“Authorization Required ....”(在成功登录后)。 1工作正常(显示预期的页面)。

我的想法已经用完,需要帮助。你能帮助我吗?提前谢谢。

 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("user", user.getText().toString()));
        nameValuePairs.add(new BasicNameValuePair("password", pass.getText().toString()));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
        httppost.addHeader("Authorization", "Basic BLA123BLA23BLA32343LSJ");

        // Execute HTTP Post Request    
        response = httpclient.execute();

       //WebView                    
                    webview_asd = new WebView(MainScreen.this);
                    webview_asd = (WebView) findViewById(R.id.webview_asd);
                    webview_asd.getSettings().setLoadsImagesAutomatically(true);
                    webview_asd.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
                    webview_asd.setWebViewClient( new WebViewClient() );
                    webview_asd.getSettings().setJavaScriptEnabled(true);


                    CookieSyncManager.createInstance (webview_asd.getContext());
                    CookieSyncManager.getInstance().sync();

                    cookies = ((AbstractHttpClient) httpclient).getCookieStore().getCookies();

                    if(cookies != null)
                    {
                        for(Cookie cookie : cookies)
                        {
                            cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();                        
                            CookieManager.getInstance().setCookie(cookie.getDomain(), cookieString);  
                        }
                    }

                    CookieSyncManager.getInstance().sync();

                webview_asd.loadURL(URL);   
                webview_asd.setVisibility(View.VISIBLE);

webview页面显示“需要授权。此服务器无法验证您是否有权访问所请求的文档。您提供了错误的凭据(例如,密码错误),或者您的浏览器不了解如何提供需要凭证。“

稍后编辑: 刚刚发现4.2.2不支持HTML5,这就是我遇到这个问题的原因。

1 个答案:

答案 0 :(得分:0)

使用setHeader。

而不是添加标题
            httppost.setHeader("Authorization",
                    "Basic BLA123BLA23BLA32343LSJ");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity httpEntity = response.getEntity();
           String html = EntityUtils.toString(httpEntity);
           //use this in webview url