[android]-如何在向服务器发送request时附加已保存的cookie数据
 
应用场景:
在开发android基于手机端+服务器端的应用时,登陆->获取用户信息->获取授权用户相关业务数据(如用户工作任务安排)
,此时就涉及到在登陆后必须把保存有用户已登陆的cookie数据(和服务器端使用cookie或session来判断用户是否登陆没关系,因为这两种方式,在客户端来说,都是通过保存cookie数据来实现的)一同发送到服务器端,不然,服务器端会认为你没有登陆,而不给提供用户工作任务安排的数据
我在工作中就遇到过这问题
一开始我是这样书写代码的
  
  
    | 01 | importjava.io.IOException; | 
 
  
  
    | 02 | importjava.net.SocketTimeoutException; | 
 
  
  
    | 03 | importjava.util.ArrayList; | 
 
  
  
    | 04 | importjava.util.List; | 
 
  
  
    | 05 | importorg.apache.http.HttpResponse; | 
 
  
  
    | 06 | importorg.apache.http.NameValuePair; | 
 
  
  
    | 07 | importorg.apache.http.client.methods.HttpPost; | 
 
  
  
    | 08 | importorg.apache.http.impl.client.AbstractHttpClient; | 
 
  
  
    | 09 | importorg.apache.http.impl.client.DefaultHttpClient; | 
 
  
  
    | 14 | HttpPost httpRequest = newHttpPost(url); | 
 
  
  
    | 16 | List<NameValuePair> params = newArrayList<NameValuePair>(); | 
 
  
  
    | 17 | params.add(newBasicNameValuePair(para, data)); | 
 
  
  
    | 21 |     httpRequest.setEntity(newUrlEncodedFormEntity(params, HTTP.UTF_8)); | 
 
  
  
    | 24 |     DefaultHttpClient defaultHttpClient = newDefaultHttpClient(); | 
 
  
  
    | 26 |     defaultHttpClient.getParams().setParameter( | 
 
  
  
    | 27 |             CoreConnectionPNames.CONNECTION_TIMEOUT, 10000); | 
 
  
  
    | 29 |     defaultHttpClient.getParams().setParameter( | 
 
  
  
    | 30 |             CoreConnectionPNames.SO_TIMEOUT, 10000); | 
 
  
  
    | 34 |     HttpResponse httpResponse = defaultHttpClient.execute(httpRequest); | 
 
  
  
    | 38 |     if(httpResponse.getStatusLine().getStatusCode() == 200) { | 
 
  
  
    | 39 |         result = EntityUtils.toString(httpResponse.getEntity()); | 
 
  
  
    | 42 | } catch(ClientProtocolException e) { | 
 
  
  
    | 43 |     System.out.println("ClientProtocolException:"+ e.toString()); | 
 
  
  
    | 46 | } catch(UnsupportedEncodingException e) { | 
 
  
  
    | 47 |     System.out.println("UnsupportedEncodingException:"+ e.toString()); | 
 
  
  
    | 49 | } catch(SocketTimeoutException e) { | 
 
  
  
    | 50 |     System.out.println("SocketTimeoutException:"+ e.toString()); | 
 
  
  
    | 52 | } catch(IOException e) { | 
 
  
  
    | 53 |     System.out.println("IOException:"+ e.toString()); | 
 
  
调用发现登陆成功后在获取工作任务时服务器提示未登陆,看来是没把cookie发送到服务器,由是找资料
修改后如下,有两个类
WebHelperResponse.java
  
  
    | 1 | packagecom.demo.utils; | 
 
  
  
    | 3 | publicclassWebHelperResponse { | 
 
  
  
    | 4 |     publicString ResponseText = ""; | 
 
  
  
    | 5 |     publicbooleanIsOk = false; | 
 
  
  
    | 6 |     publicbooleanIsError = false; | 
 
  
  
    | 7 |     publicString ErrMsg = ""; | 
 
  
WebHelper.java
  
  
    | 001 | packagecom.nt.android.app.slorders.utils; | 
 
  
  
    | 003 | importjava.io.IOException; | 
 
  
  
    | 004 | importjava.net.SocketTimeoutException; | 
 
  
  
    | 005 | importjava.util.ArrayList; | 
 
  
  
    | 006 | importjava.util.List; | 
 
  
  
    | 007 | importorg.apache.http.HttpResponse; | 
 
  
  
    | 008 | importorg.apache.http.NameValuePair; | 
 
  
  
    | 009 | importorg.apache.http.client.CookieStore; | 
 
  
  
    | 010 | importorg.apache.http.client.entity.UrlEncodedFormEntity; | 
 
  
  
    | 011 | importorg.apache.http.client.methods.HttpPost; | 
 
  
  
    | 012 | importorg.apache.http.impl.client.AbstractHttpClient; | 
 
  
  
    | 013 | importorg.apache.http.impl.client.DefaultHttpClient; | 
 
  
  
    | 014 | importorg.apache.http.message.BasicNameValuePair; | 
 
  
  
    | 015 | importorg.apache.http.params.CoreConnectionPNames; | 
 
  
  
    | 016 | importorg.apache.http.protocol.HTTP; | 
 
  
  
    | 017 | importorg.apache.http.util.EntityUtils; | 
 
  
  
    | 019 | publicclassWebHelper { | 
 
  
  
    | 021 |     privatestaticCookieStore cookieStore; | 
 
  
  
    | 023 |     publicstaticWebHelperResponse postJson(String url, String data) { | 
 
  
  
    | 024 |         returnpostData(url, data, "jsondata"); | 
 
  
  
    | 027 |     publicstaticWebHelperResponse postXml(String url, String data) { | 
 
  
  
    | 028 |         returnpostData(url, data, "xmldata"); | 
 
  
  
    | 032 |     publicstaticWebHelperResponse postData(String url, String data, | 
 
  
  
    | 034 |         WebHelperResponse m_response = newWebHelperResponse(); | 
 
  
  
    | 035 |         m_response.IsOk = false; | 
 
  
  
    | 036 |         m_response.IsError = false; | 
 
  
  
    | 040 |         HttpPost httpRequest = newHttpPost(url); | 
 
  
  
    | 043 |         List<NameValuePair> params = newArrayList<NameValuePair>(); | 
 
  
  
    | 044 |         params.add(newBasicNameValuePair(para, data)); | 
 
  
  
    | 048 |             httpRequest.setEntity(newUrlEncodedFormEntity(params, HTTP.UTF_8)); | 
 
  
  
    | 051 |             DefaultHttpClient defaultHttpClient = newDefaultHttpClient(); | 
 
  
  
    | 053 |             defaultHttpClient.getParams().setParameter( | 
 
  
  
    | 054 |                     CoreConnectionPNames.CONNECTION_TIMEOUT, 10000); | 
 
  
  
    | 056 |             defaultHttpClient.getParams().setParameter( | 
 
  
  
    | 057 |                     CoreConnectionPNames.SO_TIMEOUT, 10000); | 
 
  
  
    | 059 |             if(cookieStore != null) { | 
 
  
  
    | 060 |                 defaultHttpClient.setCookieStore(cookieStore); | 
 
  
  
    | 064 |             HttpResponse httpResponse = defaultHttpClient.execute(httpRequest); | 
 
  
  
    | 065 |             intresponseStatusCode = httpResponse.getStatusLine() | 
 
  
  
    | 067 |             System.out.println("response statuscode:"+ responseStatusCode); | 
 
  
  
    | 070 |             if(responseStatusCode == 200) { | 
 
  
  
    | 071 |                 result = EntityUtils.toString(httpResponse.getEntity()); | 
 
  
  
    | 073 |                 cookieStore = ((AbstractHttpClient) defaultHttpClient) | 
 
  
  
    | 076 |                 m_response.IsError = false; | 
 
  
  
    | 077 |                 m_response.IsOk = true; | 
 
  
  
    | 078 |                 m_response.ResponseText = result; | 
 
  
  
    | 080 |             } elseif(responseStatusCode == 404) { | 
 
  
  
    | 082 |                 m_response.IsError = true; | 
 
  
  
    | 083 |                 m_response.IsOk = false; | 
 
  
  
    | 084 |                 System.out.println("404:page not found."); | 
 
  
  
    | 085 |                 m_response.ErrMsg = "不存在此服务,请检查服务器地址配置"; | 
 
  
  
    | 087 |                 m_response.IsError = true; | 
 
  
  
    | 088 |                 m_response.IsOk = false; | 
 
  
  
    | 089 |                 System.out.println("error,responsestatuscode:" | 
 
  
  
    | 090 |                         + responseStatusCode); | 
 
  
  
    | 091 |                 m_response.ErrMsg = "服务出错"; | 
 
  
  
    | 093 |         } catch(SocketTimeoutException e) { | 
 
  
  
    | 094 |             System.out.println("ClientProtocolException:"+ e.toString()); | 
 
  
  
    | 097 |             m_response.IsError = true; | 
 
  
  
    | 098 |             m_response.IsOk = false; | 
 
  
  
    | 099 |             m_response.ErrMsg = "连接错误:未能连接到服务器!"; | 
 
  
  
    | 101 |         } catch(IOException e) { | 
 
  
  
    | 102 |             System.out.println("ClientProtocolException:"+ e.toString()); | 
 
  
  
    | 105 |             m_response.IsError = true; | 
 
  
  
    | 106 |             m_response.IsOk = false; | 
 
  
  
    | 107 |             m_response.ErrMsg = "错误:服务器未能提供服务!"; | 
 
  [android]-如何在向服务器发送request时附加已保存的cookie数据,布布扣,bubuko.com
[android]-如何在向服务器发送request时附加已保存的cookie数据
原文:http://www.cnblogs.com/daishuguang/p/3775004.html