0

so I need show specific screen when there is a request timeout. I am using this code to init the retrofit

object RetrofitServiceGenerator {

    private val loggingInterceptor = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)

    private val okHttpClient = OkHttpClient.Builder()
        .callTimeout(7, TimeUnit.SECONDS)
        .addInterceptor(loggingInterceptor)
        .build()

    private var retrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .client(okHttpClient)
        .build()


    fun <T> getInstance(APIType: Class<T>) : T {

        return retrofit.create(APIType)
    }

}

so after 7 seconds, I want to show a specific screen, but the problem is, I don't know, when I get notified if there is a request timeout in my code. for example in my code below

    val call = restaurantService.getRestaurantDetail(restaurantID)

    call.enqueue(object: Callback<Restaurant>{
        override fun onFailure(call: Call<Restaurant>, t: Throwable) {


        }

        override fun onResponse(call: Call<Restaurant>, response: Response<Restaurant>) {


        }


    })

java or kotlin are ok

sarah
  • 3,819
  • 4
  • 38
  • 80
  • 2
    Is this the answer you want?https://stackoverflow.com/questions/29921667/retrofit-2-catch-connection-timeout-exception – Genesis Mar 11 '20 at 05:44

1 Answers1

-1

//Define a listener in your web service instance:

public interface OnConnectionTimeoutListener 
{
void onConnectionTimeout();
}

Add an interceptor to your web service:

public WebServiceClient() {
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(10, TimeUnit.SECONDS);
client.setReadTimeout(30, TimeUnit.SECONDS);
client.interceptors().add(new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        return onOnIntercept(chain);
    }
});
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .client(client)
        .build();
webService = retrofit.create(WebService.class);
}

//Enclose your intercep code with try-catch block and notify listener when exception happens:

private Response onOnIntercept(Chain chain) throws IOException {
try {
    Response response = chain.proceed(chain.request());
    String content = UtilityMethods.convertResponseToString(response);
    Log.d(TAG, lastCalledMethodName + " - " + content);
    return response.newBuilder().body(ResponseBody.create(response.body().contentType(), content)).build();
}
catch (SocketTimeoutException exception) {
    exception.printStackTrace();
    if(listener != null)
        listener.onConnectionTimeout();
}

return chain.proceed(chain.request());
}
Apps Maven
  • 1,314
  • 1
  • 4
  • 17
  • 2
    This answer is copied word to word from https://stackoverflow.com/questions/29921667/retrofit-2-catch-connection-timeout-exception – Madhu Bhat Mar 11 '20 at 06:21