我在使用OKhttpClient时设置了超时:
client = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.SECONDS)//单位是秒
.readTimeout(20, TimeUnit.SECONDS)
.build();
但是在发生超时的时候,我不知道该如何捕获超时的异常,我有尝试使用 if(e.getCause().equals(SocketTimeoutException.class)时发现e.getCause始终未Null 请问我改如何捕获此类异常呢?
捕获异常不都是try catch么。。
@Override
public void onFailure(Call call, Throwable t) {
if(t instanceof SocketTimeoutException){
message = "Socket Time out. Please try again.";
}
}
这个问题太简单了,当你使用OKHttp异步去访问网络的时候不是callback不是要实现两个方法吗?其中有一个方法是onFail(Call call, IOException e)你只要判断e.getCause().equals(SocketTimeoutException.class)就行了