Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit 2 - Is it possible to get the size of a JSON that I'm sending and receiving?

I need to build a traffic monitor on my Android app, and I need to have stored the size of all json that I'm sending and receiving through retrofit. Using log I can see the actual size of it, but I haven't find a way to get this information so I could save it. I can't get the response.raw either since it's already been parsed to my classes. Is there any way to achieve that?

EDIT: Marked vadkou answer as the best one.

Instead of creating a new interceptor, I passed the lamda expression:

 httpClient.addInterceptor( chain -> {
        okhttp3.Request request = chain.request();
        okhttp3.Response response = chain.proceed(request);
        if(request.body()!=null) {
            long requestLength = request.body().contentLength();
            Log.e("SERVICE GENERATOR", " CONTENT LENGTH" + requestLength);

        }
        long responseLength = response.body().contentLength();
        Log.e("SERVICE GENERATOR", " RESPONSE LENGTH" + responseLength);

        return response;

    });
like image 768
julioribeiro Avatar asked Mar 09 '23 19:03

julioribeiro


2 Answers

Retrofit 2 uses OkHttp internally, and you could configure OkHttp without having to resort to getting raw HTTP response as in Vaiden's answer by adding a custom Interceptor while building an adapter as follows:

private Retrofit createRetrofit() {
       return new Retrofit.Builder()
            .baseUrl(END_POINT)
       //     .addConverterFactory(...)
       //     .addCallAdapterFactory(...)
            .client(createClient())
            .build();
}

private OkHttpClient createClient() {
        OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
        okHttpClientBuilder.addInterceptor(createYourInterceptor());
        return okHttpClientBuilder.build();
}

The Interceptor interface among other things allows you to access request body for every request you make.

@Override
public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
            // do what you want with request.body().contentLength();
        return chain.proceed(request);
}
like image 61
vadkou Avatar answered Apr 05 '23 23:04

vadkou


For this you need to create custom interecptor please reffere below example

import java.io.IOException; 
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;


public class CustomIntercepter implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();///
        Response response = chain.proceed(request);


        // for request size 

        long requestLength = request.body().contentLength();

        // for response size 
        long responseLength = response.body().contentLength();


        return response;
    }
}

`

Now Create Retrofit object

OkHttpClient provideOkHttpClient(CustomIntercepter customIntercepter) {
    OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
    okHttpClient.cache(cache);
    okHttpClient.addInterceptor(customIntercepter);
    return okHttpClient.build();
}


Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
    Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(SERVER_URL)
            .client(okHttpClient)
            .build();
    return retrofit;
}
like image 31
jaffar Avatar answered Apr 05 '23 23:04

jaffar