私は前にレスポンスとして返されるJSONレスポンスを編集するためのインターセプタ(OkHttp 3.2 &レトロフィット2)を実装しようとしてきました。データを要求するサーバーは、成功またはエラーに応じて異なるデータを返すため、オブジェクトをマップするのが難しくなります。Androidのレトロフィット2、編集応答のためのaddInterceptor&addNetworkInterceptorとの違い
RetrofitにNetworkInterceptorとしてインターセプタを追加していましたが、返される文字列にはフォーマットがありませんでした。 responseString
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
try {
final String responseString = new String(response.body().bytes());
LOGD("OkHttp-NET-Interceptor", "Response: " + responseString);
String newResponseString = editResponse(responseString);
LOGD("OkHttp-NET-Interceptor", "Response edited: " + newResponseString);
return response.newBuilder()
.body(ResponseBody.create(response.body().contentType(), newResponseString))
.build();
}catch (Exception ex){
return response;
}
}
は、任意の分かりやすいフォーマットせずに文字列を持っていました。
通常のインターセプタに変更した後、文字列の形式はJSONObjectに変換できました。
の回答はとなりますか?
なぜこの行新しい文字列(response.body()。バイト());別のコンテンツを返しますか?
あなたの説明をお寄せいただき、ありがとうございました。しかし、NetworkInterceptorが読み込み可能な文字列を返さず、通常のインターセプタが返すのはなぜですか? –
@レオン私はいくつかの詳細を説明しようと私の答えに追加しました – roarster