Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Retrofit 2, differences between addInterceptor & addNetworkInterceptor for editing responses

I've been trying to implement an interceptor ( OkHttp 3.2 & Retrofit 2 ) for editing the JSON response before is returned as response. The server we request data returns different data dependes on success or error and that makes difficult to map the objects.

I was trying to do it by adding the interceptor to Retrofit as a NetworkInterceptor, however the string returned had no format.

@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;         }     } 

responseString had a string without any understandable format.

After changing to the normal interceptor, the string had format a it was able to convert to JSONObject.

Could tell me someone which are the differences between the responses?

why this line new String(response.body().bytes() ); return different content?

like image 398
Jose M Lechon Avatar asked May 09 '16 15:05

Jose M Lechon


People also ask

What is the difference between interceptor and network interceptor?

Application Interceptors: These are interceptors that are added between the Application Code(our written code) and the OkHttp Core Library. These are the ones that we add using addInterceptor(). Network Interceptors: These are interceptors that are added between the OkHttp Core Library and the Server.

What is the use of interceptor in retrofit Android?

Interceptors, according to the documentation, are a powerful mechanism that can monitor, rewrite, and retry the API call. So, when we make an API call, we can either monitor it or perform some tasks. In a nutshell, Interceptors function similarly to airport security personnel during the security check process.


1 Answers

The differences are in the names. NetworkInterceptor hooks in at the network level and is an ideal place to put retry logic and anything that doesn't rely on the actual content of the response.

If what you do depends on the contents of the response (like in your case), using a ApplicationInterceptor is more useful, as it gives you the response after it's been processed by any other moving parts you may have such as a JSON deserializer. Otherwise you would have to implement the JSON deserializing yourself inside the NetworkInterceptor which doesn't make much sense considering it's done for you by Retrofit.

Clarification

Square have this useful diagram on their wiki that shows where each type of interceptor sits

interceptor diagram

Thus, the reason you receive a readable string in the ApplicationInterceptor is because Square are trying to de-couple the purposes of the two interceptor types. They don't think you should be making any application dependent decisions in the NetworkInterceptor, and so they don't provide an easy way for you to access the response string. It is possible to get ahold of, but like I said, they don't want you to make decisions that depend on the content of the response - rather, they want you to make decisions based or the network state, or headers etc.

The ApplicationInterceptor is where they want you to make decisions dependent upon the contents of the response, so they provide easier methods to access the content of the response so that you can make informed decisions to retry, or as they detail in their wiki, rewrite responses (which I believe is what you're trying to do).

like image 81
roarster Avatar answered Oct 09 '22 00:10

roarster