I use the library Retrofit, when the Internet is slow, then when sending photos to the server, a time out error occurs. I tried this way to increase the wait time, but still the default wait time is 10 seconds. Please help me what I am doing wrong.
Here is my code:

public class RestClient { private UploadService uploadService; private String URL ="http://10.0.2.2:7899/api/"; public RestClient(){ Gson localGson = new GsonBuilder().create(); final OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setReadTimeout(60, TimeUnit.SECONDS); okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS); this.uploadService = ((UploadService) new RestAdapter.Builder() .setEndpoint(URL) .setClient(new OkClient(okHttpClient)) .setEndpoint(URL) .setConverter(new GsonConverter(localGson)) .setRequestInterceptor(new RequestInterceptor() { public void intercept(RequestFacade requestFacade) { if (URL.contains("10.0.2.2")) { requestFacade.addHeader("Host", "localhost"); } } }) .build().create(UploadService.class)); } public UploadService getService() { Log.e("ServiceName", this.uploadService.toString()); return this.uploadService; } } 

    1 answer 1

    Please specify the version of okhttp. There with 3.0 feature is new - you need to do it through the builder.

    those. in your case will be:

     public class RestClient { private UploadService uploadService; private String URL ="http://10.0.2.2:7899/api/"; public RestClient(){ Gson localGson = new GsonBuilder().create(); final OkHttpClient okHttpClient = new OkHttpClient.Builder() .readTimeout(60, TimeUnit.SECONDS) .connectTimeout(60, TimeUnit.SECONDS) .build(); this.uploadService = ((UploadService) new RestAdapter.Builder() .setEndpoint(URL) .setClient(new OkClient(okHttpClient)) .setEndpoint(URL) .setConverter(new GsonConverter(localGson)) .setRequestInterceptor(new RequestInterceptor() { public void intercept(RequestFacade requestFacade) { if (URL.contains("10.0.2.2")) { requestFacade.addHeader("Host", "localhost"); } } }) .build().create(UploadService.class)); } public UploadService getService() { Log.e("ServiceName", this.uploadService.toString()); return this.uploadService; } } 

    more details can be found here

    • OkHttp version 2.6.0. - iS_Tom
    • @iS_Tom and the version of a retrofit? - Victor
    • Retrofit version 1.9.0 - iS_Tom
    • @iS_Tom may sound silly, but try lowering the version of okHttp to 2.5.0 - Victor
    • 2
      added this: "compile 'com.jakewharton.retrofit: retrofit1-okhttp3-client: 1.0.2'", and used your example with okHttp3, and changed the line where there was an error accessing this: ".setClient (new Ok3Client ( okHttpClient)) "and it worked. - iS_Tom