设置Cookies WebView Android api 14

时间:2013-12-16 10:59:32

标签: android cookies webview

我的cookies,webview有问题。 Webview未加载从DefaultClient获取的Cookie。 该代码适用于早期版本的API 14,但不适用于更高版本。 我没有错误,但由于缺少Cookie,Webview中的页面未加载。

private CookieManager cookieManager;
private class Connection extends AsyncTask<Oauth2Parameters,Void,HttpResponse> {
        @Override
        protected void onPreExecute() {
            CookieSyncManager.createInstance(AuthorizeTokenFlow2.this);
            cookieManager = CookieManager.getInstance();    
            SystemClock.sleep(500);
        }             
        @Override
        protected HttpResponse doInBackground(Oauth2Parameters... arg0) {
            HttpResponse response=null;
            try {
                //Execute request with DefaultClientHTTP
                response = getAuthentication(arg0[0].getAppId(),client);                
            } catch (Exception e) {
                e.printStackTrace();
            }           
            return response;
        }        
        @Override
        protected void onPostExecute(HttpResponse result) {
            try {   
                String html=Utility.extractBodyResponse(result);                
                managementCacheCookie(client);
                webView = (WebView) findViewById(R.id.webView1);        
                WebSettings webSettings = webView.getSettings();
                webSettings.setJavaScriptEnabled(true);
                webSettings.setBuiltInZoomControls(true);   
                webView.setWebViewClient(new WebViewClient(){
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        view.loadUrl(url);
                        return true;                
                    }                     
                }); 
                webView.loadData(html, "text/html", null);          
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
    private void managementCacheCookie(DefaultHttpClient client)
    {
        List<Cookie> cookies = client.getCookieStore().getCookies();         
        if (! cookies.isEmpty()){ 

            for (Cookie cookie : cookies){                 
                Cookie sessionInfo = cookie;     
                String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue() + "; domain=" + sessionInfo.getDomain();               
                cookieManager.setCookie(sessionInfo.getDomain(), cookieString);
            }           
            CookieSyncManager.getInstance().sync();
           }
    }  
    private  HttpResponse getAuthentication(String idDevice,DefaultHttpClient client) throws Exception {        
    URI website = new URI(Configuration.authorizeAddress); 
    HttpGet request = new HttpGet();
    request.addHeader("Authorization", "idDevice "+idDevice);
    request.setURI(website);
    HttpResponse response;
    try{
        response = client.execute(request);   
    }catch(HttpHostConnectException htt)
    {
        htt.printStackTrace();
        throw htt;
    }            
    return response;
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我解决了,我使用:

String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue();
cookieManager.setCookie(sessionInfo.getDomain(), cookieString);

我从Cookie中删除了域名。 我还补充道:

cookieManager.setAcceptCookie(true);