【原创】「Android」Retrofit2.0+okhttp3
2019-03-11 本文已影响0人
叨码
前言不搭后语:
自上次简书要求更新发布文章需要用户绑定微信和手机号,着实被恶心了一下下,也说过不再此处更新了。但现在想想,谁不是已经被扒的底裤都没了,这点隐私的抗争无非是给施暴者增加点情趣而已,穿上裤子,生活还得继续,文章还得继续写,此刻真香。
正题
一般设置超时时间的方法:
OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
builder.readTimeout(20, TimeUnit.SECONDS);
builder.connectTimeout(10, TimeUnit.SECONDS).build();
Retrofit retrofit =new Retrofit.Builder().baseUrl("")
.client(client).build();
retrofit.create(ApiService.class);
但某些时候,我们需要针对单个的接口进行动态设置超时时间。网上能找到的答案基本都是拦截器+反射实现:
- 创建一个新的OkHttpClient,重新设置超时时间。
- 通过反射机制拿到对应的属性重新设置起时间值。
以上两种方法,第一种没试过,因为觉得不够优雅。
第二种网上的做法如下,是我根据网上看到的实现的:
/**
* Created by ing on 2019/3/6
* 动态设置接口请求超时时间
*/
public class DynamicTimeoutInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request oldRequest = chain.request();
setDynamicTimeout(oldRequest,okhttpclient);//这里是你当前的okhttpclient对象
Request newRequest = oldRequest.newBuilder()
.method(oldRequest.method(), oldRequest.body())
.url(oldRequest.url())
.build();
return chain.proceed(newRequest);
}
/**
* 根据所需接口、进行动态设置网络超时时间
*
* @param oldRequest
// * @param retrofit
*/
private void setDynamicTimeout(Request oldRequest,OkHttpClient client) {
final String questUrl = oldRequest.url().url().toString();
try {
//设置连接超时,注意混淆规则,避免字段被混淆
boolean isCloseApi = questUrl.contains(PayApi.API_CLOSEORDER);
boolean isPollingApi = questUrl.contains(PayApi.API_POLLING);
if (isCloseApi||isPollingApi){
Field readTimeoutField = client.getClass().getDeclaredField("readTimeout");
readTimeoutField.setAccessible(true);
//设置读写超时
Field connectTimeoutField = client.getClass().getDeclaredField("connectTimeout");
connectTimeoutField.setAccessible(true);
//过滤接口,重新设置超时时间
if (isCloseApi) {
connectTimeoutField.setInt(client, TimeOut.CLOSE_CONNECT);
readTimeoutField.setInt(client, TimeOut.CLOSE_READ);
} else if (isPollingApi) {
readTimeoutField.setInt(client, TimeOut.POLLING_READ );
}else {
readTimeoutField.setInt(client, TimeOut.DEFALT_READ );
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
以上方法测试无效,究其原因是因为反射拿到的属性是final的,所以即便拿到也无法修改,这里感谢评论里epitomizelu
解惑。
后来查看chain 接口内容,发现其内就已提供了对应的超时时间设置接口方法:
/**
* Observes, modifies, and potentially short-circuits requests going out and the corresponding
* responses coming back in. Typically interceptors add, remove, or transform headers on the request
* or response.
*/
public interface Interceptor {
Response intercept(Chain chain) throws IOException;
interface Chain {
Request request();
Response proceed(Request request) throws IOException;
/**
* Returns the connection the request will be executed on. This is only available in the chains
* of network interceptors; for application interceptors this is always null.
*/
@Nullable Connection connection();
Call call();
int connectTimeoutMillis();
Chain withConnectTimeout(int timeout, TimeUnit unit); //重点在这
int readTimeoutMillis();
Chain withReadTimeout(int timeout, TimeUnit unit);//重点在这
int writeTimeoutMillis();
Chain withWriteTimeout(int timeout, TimeUnit unit);//重点在这
}
}
可以看到对应的三种超时时间都有提供,哦了,那我们就通过这个来设置一下,最终代码如下:
/**
* Created by ing on 2019/3/6
* 动态设置接口请求超时时间
*/
public class DynamicTimeoutInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request reequest = chain.request();
String questUrl = reequest.url().toString();
boolean isCloseApi = questUrl.contains(PayApi.API_CLOSEORDER);
boolean isPollingApi = questUrl.contains(PayApi.API_POLLING);
if (isCloseApi){
return chain.withConnectTimeout(TimeOut.CLOSE_CONNECT,TimeUnit.SECONDS)
.withReadTimeout(TimeOut.CLOSE_READ,TimeUnit.SECONDS)
.proceed(reequest);
}else if (isPollingApi){
return chain.withReadTimeout(TimeOut.POLLING_READ,TimeUnit.SECONDS)
.proceed(reequest);
}
return chain.proceed(reequest);
}
}
最后别忘了,将此拦截器添加上:
.addInterceptor(new DynamicTimeoutInterceptor())
然后测试 完美!!
mario.gif