如何在OkHttp响应中更改正文? [英] How to change body in OkHttp Response?
本文介绍了如何在OkHttp响应中更改正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用改装。要捕获响应我正在使用Interceptor:
I'm using retrofit. To catch response i'm using Interceptor:
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.interceptors().add(myinterceptor);
这里是拦截器代码:
new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
if (path.equals("/user")){
String stringJson = response.body().string();
JSONObject jsonObject = new JSONObject(stringJson);
jsonObject.put("key",1);
//here I need to set this new json to response and then return this response
如何在OkHttp响应中更改正文?
How to change body in OkHttp Response?
推荐答案
添加此
MediaType contentType = response.body().contentType();
ResponseBody body = ResponseBody.create(contentType, jsonObject);
return response.newBuilder().body(body).build();
。 jsonObject
是您要返回的已修改JSON。
after your response modification. jsonObject
is the modified JSON you want to return.
这篇关于如何在OkHttp响应中更改正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文