Interceptor修改服务器返回的数据
程序员文章站
2022-05-23 10:54:30
...
package
import android.util.Log;
import java.io.IOException;
import java.nio.charset.Charset;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;
/**
* @author liushengwei 请求拦截器
* @description: https://github.com/lsw8569013
* @date :2020-02-14 14:03
*/
public class CommonRequestInterceptor implements Interceptor {
private INetWorkInfo netWorkInfo;
public CommonRequestInterceptor(INetWorkInfo netWorkInfo) {
this.netWorkInfo = netWorkInfo;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
// 可以添加公共的请求投 token Access_Token
String token = netWorkInfo.getToken();
Request request = null;
if (token != null) {
request = builder.addHeader("Access_Token", token)
.build();
}else{
request = builder .build();
}
//打印返回信息
Response response = chain.proceed(request);
if (BuildConfig.DEBUG) {
Log.e("lsw", "response返回参数" + response.toString());
}
ResponseBody responseBody = response.body();
long contentLength = responseBody.contentLength();
BufferedSource source = responseBody.source();
source.request(Integer.MAX_VALUE); // Buffer the entire body.
Buffer buffer = source.buffer();
String backSource = buffer.clone().readString(Charset.forName("UTF-8"));
if(BuildConfig.DEBUG){
if (contentLength != 0) {
Log.e("lsw", "服务器返回数据 =:" + backSource);
}
}
BaseBeanCheck bean = new Gson().fromJson(backSource, BaseBeanCheck.class);
if(bean.getData() instanceof String){
bean.setData(null);
String replace = new Gson().toJson(bean);
if(BuildConfig.DEBUG){
Log.e("lsw", "服务器修改后 返回数据 =:" + replace);
}
Response build = response.newBuilder().body(ResponseBody.create(responseBody.contentType(), replace)).build();
return build;
}
return response;
}
}
上一篇: 怎么将个位十位百位.分开显示