Retrofit2 Okhttp3 catch TimeOut

女生的网名这么多〃 提交于 2019-12-12 18:36:15

问题


I got a client like this:

public enum RestClient {

INSTANCE;
private static final int CONNECTION_TIMEOUT = 10;
private static final int READ_TIMEOUT = 30;
private static final int WRITE_TIMEOUT = 30;
private final Rest restClient;

private RestClient() {
    restClient = new Retrofit.Builder()
            .baseUrl(App.getContext().getResources().getString(R.string.EP))
            .addConverterFactory(JacksonConverterFactory.create())
            .client(new okhttp3.OkHttpClient.Builder()
                    .connectTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS)
                    .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
                    .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                    .build())
            .build()
            .create(Rest.class);
}

public Rest getRestClient() {return restClient;}

}

and i use this like this:

RestClient.INSTANCE.getRestClient().getAndroidConf().enqueue(new Callback<List<Config>>() {
        @Override
        public void onResponse(Call<List<Config>> call, Response<List<Config>> response) {
            //
        }

        @Override
        public void onFailure(Call<List<Config>> call, Throwable t) {
            //
        }
    });

I've recently updated to retrofit 2 and okhttp3, the problem is that i was expecting that when a timeout occures it enter onFailure callback, as the old retrofit did.

Any suggestions? Thanks

来源:https://stackoverflow.com/questions/37572895/retrofit2-okhttp3-catch-timeout

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