安卓开发中,http 网络请求的框架有很多,有 HttpUrlConnection
、 Volley
、 Okhttp
很多种,现在由于 retrofit 和 rxjava 的出现, Okhttp 的使用越来越多,因为 Retrofit 也仅仅是在 Okhttp 做的一层封装,那么需求上需要我们在每一个 URL 请求中添加一个固定的参数时候,或者将请求的消息头统一改为我们需要的形式,我们要怎么实现呢?
我们用过 OkHttp 的都知道它独特而且强大的拦截器功能,比如 提供的 HttpLoggingInterceptor()
,我们也可以通过自定义 拦截器来实现上述的公共参数需求。首先我们实现 Interceptor
接口
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();
}
}
在 intercept (Chain chain)
方法中 Chain 对象可以拿到当前请求的 Request
对象,然后我们可以对Request
做二次处理,最后生成我们需要的请求,然后再通过网络发送请求到服务端,这样就完成了一次拦截。由于代码很简单,我们先介绍使用方法,源码贴在了后面:
//添加post公共请求参数 Constants.PACKAGE 和 Constants.CFROM
BasicParamsInterceptor basicParamsInterceptor = new BasicParamsInterceptor.Builder()
.addParam("from", "android") //添加公共参数到 post 请求体
.addQueryParam("version","1") // 添加公共版本号,加在 URL 后面
.addHeaderLine("X-Ping: Pong") // 示例: 添加公共消息头
.addParamsMap(map) // 可以添加 Map 格式的参数
.build();
然后在 OkHttpClient
中添加拦截器就完成了
okHttpClient = new OkHttpClient.Builder()
.readTimeout(READ_TIME_OUT, TimeUnit.MILLISECONDS)
.connectTimeout(CONNECT_TIME_OUT, TimeUnit.MILLISECONDS)
.writeTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS)
.addInterceptor(basicParamsInterceptor) // 添加公共参数拦截器
.addInterceptor(logInterceptor)
.addInterceptor(noNetInterceptor)
.addNetworkInterceptor(cacheInterceptor)
.retryOnConnectionFailure(true)
.cache(cache)
.build();
整个拦截器源码如下:
/**
* Created by : Liu
* date: 2017/2/16 14:04 <br/>
*/
public class BasicParamsInterceptor implements Interceptor {
Map<String, String> queryParamsMap = new HashMap<>(); // 添加到 URL 末尾,Get Post 方法都使用
Map<String, String> paramsMap = new HashMap<>(); // 添加到公共参数到消息体,适用 Post 请求
Map<String, String> headerParamsMap = new HashMap<>(); // 公共 Headers 添加
List<String> headerLinesList = new ArrayList<>(); // 消息头 集合形式,一次添加一行
// 私有构造器
private BasicParamsInterceptor() {}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request.Builder requestBuilder = request.newBuilder();
// process header params inject
Headers.Builder headerBuilder = request.headers().newBuilder();
// 以 Entry 添加消息头
if (headerParamsMap.size() > 0) {
Iterator iterator = headerParamsMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
headerBuilder.add((String) entry.getKey(), (String) entry.getValue());
}
}
// 以 String 形式添加消息头
if (headerLinesList.size() > 0) {
for (String line: headerLinesList) {
headerBuilder.add(line);
}
requestBuilder.headers(headerBuilder.build());
}
// process header params end
// process queryParams inject whatever it's GET or POST
if (queryParamsMap.size() > 0) {
request = injectParamsIntoUrl(request.url().newBuilder(), requestBuilder, queryParamsMap);
}
// process post body inject
if (paramsMap.size() > 0) {
if (canInjectIntoBody(request)) {
FormBody.Builder formBodyBuilder = new FormBody.Builder();
for(Map.Entry<String, String> entry : paramsMap.entrySet()) {
formBodyBuilder.add((String) entry.getKey(), (String) entry.getValue());
}
RequestBody formBody = formBodyBuilder.build();
String postBodyString = bodyToString(request.body());
postBodyString += ((postBodyString.length() > 0) ? "&" : "") + bodyToString(formBody);
requestBuilder.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=UTF-8"), postBodyString));
}
}
request = requestBuilder.build();
return chain.proceed(request);
}
/**
* 确认是否是 post 请求
* @param request 发出的请求
* @return true 需要注入公共参数
*/
private boolean canInjectIntoBody(Request request) {
if (request == null) {
return false;
}
if (!TextUtils.equals(request.method(), "POST")) {
return false;
}
RequestBody body = request.body();
if (body == null) {
return false;
}
MediaType mediaType = body.contentType();
if (mediaType == null) {
return false;
}
if (!TextUtils.equals(mediaType.subtype(), "x-www-form-urlencoded")) {
return false;
}
return true;
}
// func to inject params into url
private Request injectParamsIntoUrl(HttpUrl.Builder httpUrlBuilder, Request.Builder requestBuilder, Map<String, String> paramsMap) {
if (paramsMap.size() > 0) {
Iterator iterator = paramsMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
httpUrlBuilder.addQueryParameter((String) entry.getKey(), (String) entry.getValue());
}
requestBuilder.url(httpUrlBuilder.build());
return requestBuilder.build();
}
return null;
}
private static String bodyToString(final RequestBody request){
try {
final RequestBody copy = request;
final Buffer buffer = new Buffer();
if(copy != null)
copy.writeTo(buffer);
else
return "";
return buffer.readUtf8();
}
catch (final IOException e) {
return "did not work";
}
}
public static class Builder {
BasicParamsInterceptor interceptor;
public Builder() {
interceptor = new BasicParamsInterceptor();
}
// 添加公共参数到 post 消息体
public Builder addParam(String key, String value) {
interceptor.paramsMap.put(key, value);
return this;
}
// 添加公共参数到 post 消息体
public Builder addParamsMap(Map<String, String> paramsMap) {
interceptor.paramsMap.putAll(paramsMap);
return this;
}
// 添加公共参数到消息头
public Builder addHeaderParam(String key, String value) {
interceptor.headerParamsMap.put(key, value);
return this;
}
// 添加公共参数到消息头
public Builder addHeaderParamsMap(Map<String, String> headerParamsMap) {
interceptor.headerParamsMap.putAll(headerParamsMap);
return this;
}
// 添加公共参数到消息头
public Builder addHeaderLine(String headerLine) {
int index = headerLine.indexOf(":");
if (index == -1) {
throw new IllegalArgumentException("Unexpected header: " + headerLine);
}
interceptor.headerLinesList.add(headerLine);
return this;
}
// 添加公共参数到消息头
public Builder addHeaderLinesList(List<String> headerLinesList) {
for (String headerLine: headerLinesList) {
int index = headerLine.indexOf(":");
if (index == -1) {
throw new IllegalArgumentException("Unexpected header: " + headerLine);
}
interceptor.headerLinesList.add(headerLine);
}
return this;
}
// 添加公共参数到 URL
public Builder addQueryParam(String key, String value) {
interceptor.queryParamsMap.put(key, value);
return this;
}
// 添加公共参数到 URL
public Builder addQueryParamsMap(Map<String, String> queryParamsMap) {
interceptor.queryParamsMap.putAll(queryParamsMap);
return this;
}
public BasicParamsInterceptor build() {
return interceptor;
}
}
}