2012-01-10 51 views
1

試圖保存cookie的值以將設備存儲在商店中,但不是每次我再次啓動應用程序時都返回空值,我不知道爲什麼我們不存儲cookie的值。Android我如何保存來自Responde DefaultHttpClient的cookies?

final HttpUriRequest request = new HttpGet(uri);     
request.setHeader("User-Agent", Sonora.USER_AGENT); 

try {      
    DefaultHttpClient client = new DefaultHttpClient();       
    if(cookieValue != null) { 
     Log.d(TAG,"COOKIE RECEBIDO E ARMAZENADO"); 

     request.setHeader("Cookie", cookieName + "=" + cookieValue); 

    } 
    HttpResponse resp = client.execute(request); 

    // Check if server response is valid 
    StatusLine status = resp.getStatusLine(); 
    if (status.getStatusCode() != Sonora.HTTP_STATUS_OK) { 
     Log.e(TAG, "HTTP error, invalid server status code: " + resp.getStatusLine()); 
     return; 
    } 

    final List<Cookie> cookies = client.getCookieStore().getCookies(); 
    int count = cookies.size(); 
    for(int i = 0; i < count; i++) { 
     final Cookie c = cookies.get(i); 
     if (c.getName().compareTo(cookieName) == 0) { 
      cookieValue = c.getValue(); 
      break; 
     } 
    } 

} catch (IOException e) { 
    Log.e(TAG, "IOError error", e); 
} 

回答