android+jqueryMobile+phonegap 开发问题一览

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
 
 
 

你可能感兴趣的:(android+jqueryMobile+phonegap 开发问题一览)