I'm using Retrofit2 on my Android app but I'm experiencing a lot of slowness in calls compared to the same calls made through a Python script. The Android smartphone and the pc running the script use the same internet connection.
For example, a GET call (406 byte) takes on average the following times:
- Android App: 525ms
- Python script: 111ms
- Safari: 109ms
The first GET call (3140 byte) has the biggest difference:
- Android App: 1723ms
- Python script or Safari: 294ms
My Retrofit API Class code:
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
class APIClient {
private static Retrofit retrofit = null;
private static String url = "http://test.mylink.com";
static Retrofit getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
}
API calls are made inside a thread
class TestClass extends Thread {
...
public void makeCall() {
Call<Proof> call1 = _apiInterface.getProof();
call1.enqueue(new Callback<Proof>() {
@Override
public void onResponse(Call<Proof> call, Response<Proof> response) {
...
}
@Override
public void onFailure(Call<Proof> call, Throwable t) {
call.cancel();
...
}
});
}
}
Has anyone had a similar problem with Retrofit?