Retrofit2 SocketTimeOutException
Asked Answered
D

2

9

I setup for Retrofit:

private Interceptor interceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (!NetworkUtil.isNetworkAvailable()) {
                request = request.newBuilder()
                        .cacheControl(CacheControl.FORCE_CACHE)
                        .build();
            }
            Response response = chain.proceed(request); //Exception here

            if (NetworkUtil.isNetworkAvailable()) {
                String cacheControl = request.cacheControl().toString();
                response = response.newBuilder()
                        .removeHeader("Pragma")
                        .removeHeader("Cache-Control")
                        .header("Cache-Control", cacheControl)
                        .build();
            } else {
                int maxStale = 60 * 60 * 24 * 7;
                response = response.newBuilder()
                        .removeHeader("Pragma")
                        .removeHeader("Cache-Control")
                        .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                        .build();
            }
            return response;
        }
    };

And I use to setup cache:

builder = new OkHttpClient.Builder()
                    .cookieJar(new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(Apploader.context)))
                    .addNetworkInterceptor(interceptor)
                    .addInterceptor(interceptor)
                    .cache(cache)
                    .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);

Sometime when server response too long My Application crash because SocketTimeOut

It break on line: Response response = chain.proceed(request); as I comment. SocketTimeOut is subclass of IOException, why my code can't catch it. How do I resolved this problem.

Darius answered 13/12, 2017 at 3:34 Comment(0)
T
4

Use OkHttpClient for more configurations, like follwing :

final OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .connectTimeout(20, TimeUnit.SECONDS)
    .writeTimeout(20, TimeUnit.SECONDS)
    .readTimeout(30, TimeUnit.SECONDS)
    .build();

and then set this okHttpClient with RetrofitBuilder,

Retrofit.Builder()
    .client(okHttpClient);
Tambourine answered 13/12, 2017 at 4:45 Comment(0)
A
1

You can use retrofit Callback method to easily handle exception that occurs during retrofit call. It has onResponse() and onFailure() override methods. If the retrofit call is succeeded then onResponse() will trigger, If the call is failure or any exception is occur then onFailure() will trigger.

You can refer below tutorials for a better idea about retrofit call backs

https://www.journaldev.com/13639/retrofit-android-example-tutorial https://www.androidhive.info/2016/05/android-working-with-retrofit-http-library/

Arnuad answered 13/12, 2017 at 5:16 Comment(2)
Hi @EKN, Please see my update. I use Interceptor to configure for Cache. How do I catch onFaild here.Darius
If you're using a custom OkHttp Interceptor, you could handle the SocketTimeOutException inside the overridden intercept() function, or by wrapping the web service client call. See the guidance here: #58697959Digestant

© 2022 - 2024 — McMap. All rights reserved.