8

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)
DevRj
  • 448
  • 8
  • 19

2 Answers2

8
private OkHttpClient getClient() {
  OkHttpClient client = new OkHttpClient.Builder()
    .connectTimeout(5, TimeUnit.MINUTES)
    .readTimeout(5, TimeUnit.MINUTES)
    .build();
  return client;
}
Yossi
  • 11,778
  • 2
  • 53
  • 66
Rudresh
  • 731
  • 1
  • 10
  • 26
  • 2
    can you please provide more details about the issue i'm having and some clarifications about your answer because i don't want to copy past without understanding the problem – DevRj Sep 19 '16 at 12:45
  • 1
    It is timeout issue if service is taking more time to get response then it will get sockettimeoutException so we need to give ConnectionTimeOut ,readTimeout – Rudresh Sep 19 '16 at 12:53
  • Do not use this if you don't want your user to wait for 5 minutes and then he gets an error message. What if server is offline and you don't check it, you simply start a download ? And then wait for an error message 5 minutes? – hocikto Jun 09 '17 at 08:38
  • @Rudresh I didn't use okhttp with Retrofit.I just used Retrofit2 in my Android App.I too got the same error-java.net.SocketTimeoutException: timeout.Can you say something – Divya Galla Mar 24 '18 at 09:45
  • That doesn't explain the issue at all, it's not just copy and pasting the solution. There should have been some explanation telling others what the actual issue was, so they could avoid it when working, rather hitting the same issue and finding the solution. – Saqib Nov 18 '22 at 20:03
0

As you can see, the error is java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference. That could probable be that you try to iterate to a null list of Persons when server doesen't response or throw other error. Check your Callback listener or Observable (if you are using RxJava).

Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
  • indeed but the main issue is that the service gets nothing when the call happens, so it's due to the SocketTimeoutException and i really don't know how the solve the problem – DevRj Sep 19 '16 at 12:51
  • Check url, probably you have wrong parameters and server gives you nothing or server have an issue. You can use Fiddler in order to see request and response from server. You can use you phone in order to listen for requests, but if server is https, you can't do that, just call in Fiddler that url. – Cătălin Florescu Sep 19 '16 at 12:54