Fati fati Fati fati - 2 months ago 57
Android Question

SocketTimeoutException android retrofit

I am building a restful client using retrofit in my android app, however i am struggling with an exception while trying to retreive data from my webservice,
the first call works with no exception but i change the params and retry to get new data i get the exception mentioned bellow

this is my service generator class

public class ServiceGenerator {

public static final String API_BASE_URL = "http://192.168.43.109:9988/Serv/";

private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();



private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());

public static <S> S createService(Class<S> serviceClass) {
Retrofit retrofit = builder.client(httpClient.build()).build();
return retrofit.create(serviceClass);
}}


and this is the stack trace i get :

W/System.err: java.net.SocketTimeoutException
W/System.err: at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
W/System.err: at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
W/System.err: at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
W/System.err: at okio.Okio$2.read(Okio.java:140)
W/System.err: at okio.AsyncTimeout$2.read(AsyncTimeout.java:238)
W/System.err: at okio.RealBufferedSource.indexOf(RealBufferedSource.java:325)
W/System.err: at okio.RealBufferedSource.indexOf(RealBufferedSource.java:314)
W/System.err: at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:210)
W/System.err: at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:184)
W/System.err: at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:125)
W/System.err: at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:775)
W/System.err: at okhttp3.internal.http.HttpEngine.access$200(HttpEngine.java:86)
W/System.err: at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:760)
W/System.err: at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:613)
W/System.err: at okhttp3.RealCall.getResponse(RealCall.java:244)
W/System.err: at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
W/System.err: at okhttp3.RealCall.execute(RealCall.java:57)
W/System.err: at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
W/System.err: at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:89)
W/System.err: at com.example.admin.theapp.ListPersonne$GetPersonnesAsyncTask.doInBackground(ListPersonne.java:418)
W/System.err: at com.example.admin.theapp.ListPersonne$GetPersonnesAsyncTask.doInBackground(ListPersonne.java:409)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err: at java.lang.Thread.run(Thread.java:818)
W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
W/System.err: at com.example.admin.theapp.ListPersonne.onCreate(ListPersonne.java:143)
W/System.err: at android.app.Activity.performCreate(Activity.java:6374)
W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2743)
W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2855)
W/System.err: at android.app.ActivityThread.access$900(ActivityThread.java:181)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:145)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6117)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Answer
 private OkHttpClient getClient() {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES)
.build();
return client;

}