在web视图Android的设置cookie [英] setting Cookies in WebView android

查看:128
本文介绍了在web视图Android的设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设定一些饼干在我的的WebView 打开与同一个会话,我有我的应用程序的浏览器。

I'm trying to set some cookies on my WebView to open a browser with the same session that I have on my app.

我读了很多答案,但他们不为我工作。我发现的唯一的解决办法是在C中的cookie数据的使用loadURL ,硬$ C $在 extraHeaders ,但预期这仅适用于该请求,并不会十个分量的会话。

I read a lot of answers but they don't work for me. The only solution I've found is in the loadUrl, hardcode the cookie data in extraHeaders, but as expected this only works for this requests, and doesn't mantain the session.

在code,我有是:

CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(mWebView.getContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
cookieManager.setCookie("http://xx.xxx.xxx.com","mid="+MySession.GetSession().sessionId+" ; Domain=.xxx.com");
cookieSyncManager.sync();

String cookie = cookieManager.getCookie("http://xx.xxx.xxx.com");

Log.d(LOGTAG, "cookie ------>"+cookie);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new TuWebViewClient());
mWebView.loadUrl("http://xx.xx.xxx.com");

的getCookie()返回正确的数据,但是当我从服务器读取的饼干,这些都是空的。哪里不对?请指教。 谢谢!

getCookie() returns the correct data, but when I read the cookies from the server, those are empty. What is wrong? Please advise. Thank you!!!

推荐答案

解决!!!!问题是web视图,我不知道发生了什么,但如果我创建了

Solved!!!! the problem is with the webView, I dont know what happend, but If I create the

WebView webView = new WebView(Activity.this);

它的工作原理。如果我读了活动的WebView与 findViewById()这是行不通的。

另外,如果你需要设置的,你从一个网站收到previously Cookie的列表。 所有你需要做的就是使用 for循环经历,并设置所有的人。它帮助我解决的情况

Also if you need to set a list of cookies that you received previously from a website. All you have to do is use a for-loop to go through and set all of them . It helped me to solve the situation

这篇关于在web视图Android的设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆