Handle timeout in OkHTTP

扶醉桌前 提交于 2019-12-11 07:59:19

问题


How can i catch the timeout in OkHTTP? Is it "called" in the onFailure method?

I would like to handle the situation in which i have bad internet connection. In case of timeout i have to enable some buttons to allow the user to try again.

// Get a handler that can be used to post to the main thread
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            hideDialog();
            e.printStackTrace();

            // handle timeouts here, enable buttons...
        }

回答1:


You can increase timeout for connection and response for your request. Use following code,

client.setConnectTimeout(15, TimeUnit.SECONDS); // connect timeout
client.setReadTimeout(15, TimeUnit.SECONDS);    // socket timeout


来源:https://stackoverflow.com/questions/40152392/handle-timeout-in-okhttp

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