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
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());
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()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With