The upgrade continues to knock down my cookies :( Android

So, when I enter the server-side application, it gives me a cookie that allows me to say “Hey, this is me again” for future requests. I used to do this using the default HttpClient classes for Android, and it worked fine. But I was told that Retrofit is much faster and saves you JSON parsing by implementing GSON, so I decided to give it a try.

I built my client and I can make requests, however my client does not remember the session key, and therefore I can not do anything. Does anyone know how to report the “accept and user cookies for future requests” update? I'm lost here! will make any offer :)

public class ApiClient{ private static final String API_URL = "http://192.168.1.25:8080"; private static RestAppApiInterface sRestAppService; public static RestAppApiInterface getRestAppApiClient() { if (sRestAppService == null) { CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(API_URL) .build(); sRestAppService = restAdapter.create(RestAppApiInterface.class); } return sRestAppService; } } 
+8
json android cookies retrofit session
source share
2 answers

You need to install a regular cookie. Since you are using Android and modify it, I suggest using OKHttp, which is better supported by updating and stream Android streams, namely:

 //First create a new okhttpClient (this is okhttpnative) OkHttpClient client = new OkHttpClient(); //create OKHTTPClient //create a cookieManager so your client can be cookie persistant CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); client.setCookieHandler(cookieManager); //finally set the cookie handler on client //OkClient is retrofit default client, ofcourse since is based on OkHttClient //you can decorate your existing okhttpclient with retrofit okClient OkClient serviceClient = new OkClient(client); //finally set in your adapter RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("Some eNdpoint") .setClient(serviceClient) .build(); 

The point of using Okhttp instead of defaultHttpClient (by apache) is that okhttp is thread safe for android and is better supported through modification.

Remember that if you create another adapter, you will need to install the same client, perhaps if you implement singleton on a client instance, you will use the same one for all your requests, keeping in the same context

Hope this helps, better

+15
source share

If you are using Retrofit 2, you can add a library:

 compile "com.squareup.okhttp3:okhttp-urlconnection:3.2.0" 

then use the following code to manage cookies when creating the OkHttp client:

 CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.cookieJar(new JavaNetCookieJar(cookieManager)); 
+12
source share

All Articles