android+jqueryMobile+phonegap 开发问题一览
2014-03-04 22:01
344 查看
1.android 使用webView获取cookie的时候报cookierejected。
httpclient的cookie处理对我来说总觉得有些复杂,在实现不同的需求时总出现各种各样的莫名其妙问题。httpclient版本为4.0.3,构造的http请求无需传递cookie值,结果log中出现大量的warn信息:
[WARN ]13:32:00,828, pool-1-thread-9, [Class]ResponseProcessCookies, Cookie rejected: "[version: 0][name: NTESLOFTSI][value: 9C5DD68981E435146CE7654DBE5107E1.bje3b11-8010][domain: .www.lofter.com][path: /][expiry: null]". Illegal domain attribute "www.lofter.com".
Domain of origin: "api.lofter.com"
查看了一些资料,大意是httpclient请求默认需要传cookie的,那么是否有办法忽略cookie的处理呢。
貌似httpclient3版本中支持这种设置,具体设置方法为:
client.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
但是httpclient4的api中没有找到相应的方法,查看官方说明有这么一个说法:
customer cookie policy,也即是支持自定义cookie policy。
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html#d5e799
具体做法是(参考以上链接):
DefaultHttpClient httpclient = new DefaultHttpClient();
CookieSpecFactory csf = new CookieSpecFactory(){
public CookieSpec newInstance(HttpParams params) {
return new BrowserCompatSpec() {
@Override
public void validate(Cookie cookie, CookieOrigin origin) throws MalformedCookieException {
}
};
}
};
httpclient.getCookieSpecs().register("easy", csf);
httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, "easy");
通过自定义的方式,发现果然解决了cookie reject的问题。由于时间问题,并没有仔细研究这块,仅作为一个参考解决方法。
2.android cookie操作
private static HashMap<String,String> CookieContiner=new HashMap<String,String>() ; /** * 保存Cookie * @param resp */ public void SaveCookies(HttpResponse httpResponse) { Header[] headers = httpResponse.getHeaders("Set-Cookie"); String headerstr=headers.toString(); if (headers == null) return; for(int i=0;i<headers.length;i++) { String cookie=headers[i].getValue(); String[]cookievalues=cookie.split(";"); for(int j=0;j<cookievalues.length;j++) { String[] keyPair=cookievalues[j].split("="); String key=keyPair[0].trim(); String value=keyPair.length>1?keyPair[1].trim():""; CookieContiner.put(key, value); } } } /** * 增加Cookie * @param request */ public void AddCookies(HttpPost request) { StringBuilder sb = new StringBuilder(); Iterator iter = CookieContiner.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); String key = entry.getKey().toString(); String val = entry.getValue().toString(); sb.append(key); sb.append("="); sb.append(val); sb.append(";"); } request.addHeader("cookie", sb.toString()); }
cookie格式:
my_name=my_value; path=/; expires=Thu, 07-Mar-13 09:15:47 GMT; domain=.sohu.com; HttpOnly
相关文章推荐
- PhoneGap&jQuery Mobile应用开发环境配置(For Android)
- phonegap+jquerymobile开发android的心得(3)
- 用PhoneGap + jQuery Mobile开发 Android应用
- 开发基于jquerymobile+phonegap+android的应用程序(1) [转]
- phonegap+jquerymobile开发android的心得(6)
- 开发基于jquerymobile+phonegap+android的应用程序(1)
- phonegap+jquerymobile开发android的心得(5)
- 用PhoneGap + jQuery Mobile开发 Android应用
- phonegap+jquerymobile开发android的心得(1)
- phonegap+jquerymobile开发android的心得(4)
- phonegap+jQuery mobile开发中碰到的问题集锦
- 开发基于jquerymobile+phonegap+android的应用程序(2)
- 使用jQuery Mobile + PhoneGap 开发Android应用程序(转)
- phonegap+jquerymobile开发android的心得(2)
- 使用phonegap进行移动跨平台在Android平台开发所遇到的问题
- android平台phonegap开发中使用EmailComposer插件发送邮件带附件失败的问题总结
- PhoneGap+jQuery mobile下,页面切换出现的闪烁问题。
- phonegap+jQuery Mobile开发应用
- 解决jquery mobile+phonegap页面切换闪屏问题
- jquery mobile + html5 手机app表单跨域提交实例(手机app开发html5 juqery moblie phonegap系列一)