Retrofit2 SocketTimeOutException

丶灬走出姿态 提交于 2019-12-12 17:31:13

问题


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.


回答1:


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);



回答2:


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/



来源:https://stackoverflow.com/questions/47785086/retrofit2-sockettimeoutexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!