1

Trying to save the value of the cookie to store the device in the store, but not every time I boot the application again return null value, I do not know why we do not store the value of the cookie.

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);
}
Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143
Maskdevil
  • 521
  • 1
  • 5
  • 6

2 Answers2

3

The Android CookieStore class is not persistent - hence your cookies are not saved anywhere and will be lost when your app stops.

You could use the Android Asynchronous Http Client (http://loopj.com/android-async-http/) which supports an optional persistent cookie store in your app’s SharedPreferences. Or you could implement your own persistent cookie class as per How to make persistent Cookies with a DefaultHttpClient in Android?.

Community
  • 1
  • 1
Torid
  • 4,176
  • 1
  • 28
  • 29
0

If you want the cookie to be saved after restarting the application, then you will need to either write it to a file or use something like SharedPreferences to store it. You can look at this link for using SharedPreferences (which is easier): http://developer.android.com/reference/android/content/SharedPreferences.html

Alex Paino
  • 250
  • 2
  • 4