Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep session using Retrofit OkHttpClient

With the DefaultHttpClient() you could do

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url_login.toURI());
HttpGet request1 = new HttpGet(url_getList.toURI());
HttpGet request2 = new HttpGet(url.getOtherList.toURI());
HttpResponse response = client.execute(request);
HttpResponse response1 = client.execute(request1);
HttpResponse response2 = client.execute(request2);

and the client would keep the session, how do I manage the following with retrofit 2

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request original = chain.request();

                    // Customize the request
                    Request request = original.newBuilder()
                            .header("Accept", "application/json")
                            .header("Authorization", "auth-token")
                            .method(original.method(), original.body())
                            .build();

                    Response response = chain.proceed(request);

                    // Customize or return the response
                    return response;
                }
            }).build();

            Retrofit.Builder builder = new Retrofit.Builder()
                            .baseUrl(BASE_URL)
                            .addConverterFactory(GsonConverterFactory.create());

            Retrofit rt = builder.client(client).build();
            RestAPI restAPI = rt.create(RestAPI.class);
            ....
            callLogin = restAPI.login();
            Call<Model> callModel = restAPI.getModel();

2 restApi call doesn´t work restApi client is not logged in

like image 490
Ignacio Garat Avatar asked Apr 19 '16 01:04

Ignacio Garat


2 Answers

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            // init cookie manager
            CookieHandler cookieHandler = new CookieManager();

            client = new OkHttpClient.Builder().addNetworkInterceptor(interceptor)
                    .cookieJar(new JavaNetCookieJar(cookieHandler))
                    .connectTimeout(10, TimeUnit.SECONDS)
                        .writeTimeout(10, TimeUnit.SECONDS)
                        .readTimeout(30, TimeUnit.SECONDS)
                        .build();

            Retrofit.Builder builder = new Retrofit.Builder()
                            .baseUrl(BASE_URL)
                            .addConverterFactory(GsonConverterFactory.create());
like image 155
Ignacio Garat Avatar answered Nov 03 '22 20:11

Ignacio Garat


In Kotlin: Add implementation "com.squareup.okhttp3:okhttp-urlconnection:3.8.1" into your build.gradle.

var cookieHandler: CookieHandler = CookieManager()

private var retrofit: Retrofit? = null
retrofit = Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .client(client)
                        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                        .build()

private val client : OkHttpClient
            private get() {
                val builder = OkHttpClient.Builder()
                builder.cookieJar(JavaNetCookieJar(cookieHandler))
                builder.connectTimeout(15, TimeUnit.SECONDS)
                builder.readTimeout(20, TimeUnit.SECONDS)
                builder.writeTimeout(20, TimeUnit.SECONDS)
                builder.retryOnConnectionFailure(true)
                return builder.build()
            }
like image 35
Sayan Manna Avatar answered Nov 03 '22 21:11

Sayan Manna