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

狂风中的少年 提交于 2020-07-18 21:04:48

问题


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;

    });

回答1:


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



回答2:


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



回答3:


You should try accessing the raw HTTP response (Get raw HTTP response with Retrofit):

You begin with a Response object.

This object has a .raw() method that returns the actual HTTP layer's reponse, in the form of an okhttp3.Response object. Calling .body() would give you a ResponseBody object, which encapsulates the raw response.

You can get the length of the response by calling .contentLength().



来源:https://stackoverflow.com/questions/44629502/retrofit-2-is-it-possible-to-get-the-size-of-a-json-that-im-sending-and-recei

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!