通过拦截器读取原始响应后,改造未在调用正文中收到响应
After reading raw response through interceptor, retrofit is not receiving respose in call body
如果我在通过拦截器返回原始响应之前添加一个拦截器来读取原始响应,我将在改造调用中遇到失败
我的拦截器代码:
Response originalResponse = chain.proceed(builder.build());
String rawJson = originalResponse.body().string();
try {
JSONObject jsonObject = new JSONObject(rawJson);
if (jsonObject.has("respCode")) {
if (jsonObject.getString("respCode").equals("E")) {
context.getSharedPreferences(AppConstants.PREF_NAME, Context.MODE_PRIVATE).edit()
.putBoolean(AppConstants.IS_LOGGED_IN_KEY, false)
.apply();
context.startActivity(new Intent(context, LoginActivity.class));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return originalResponse;
里面没有编译错误。
没有这个 try-catch 块一切正常。
任何建议都会有所帮助
您可能会在 HttpLoggingInterceptor
中看到示例
ResponseBody responseBody = originalResponse.body();
BufferedSource source = responseBody.source();
source.request(Long.MAX_VALUE); // Buffer the entire body.
Buffer buffer = source.buffer();
String rawJson = buffer.clone().readString(Charset.forName("UTF-8"));
如果我在通过拦截器返回原始响应之前添加一个拦截器来读取原始响应,我将在改造调用中遇到失败 我的拦截器代码:
Response originalResponse = chain.proceed(builder.build());
String rawJson = originalResponse.body().string();
try {
JSONObject jsonObject = new JSONObject(rawJson);
if (jsonObject.has("respCode")) {
if (jsonObject.getString("respCode").equals("E")) {
context.getSharedPreferences(AppConstants.PREF_NAME, Context.MODE_PRIVATE).edit()
.putBoolean(AppConstants.IS_LOGGED_IN_KEY, false)
.apply();
context.startActivity(new Intent(context, LoginActivity.class));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return originalResponse;
里面没有编译错误。 没有这个 try-catch 块一切正常。
任何建议都会有所帮助
您可能会在 HttpLoggingInterceptor
中看到示例ResponseBody responseBody = originalResponse.body();
BufferedSource source = responseBody.source();
source.request(Long.MAX_VALUE); // Buffer the entire body.
Buffer buffer = source.buffer();
String rawJson = buffer.clone().readString(Charset.forName("UTF-8"));