Retrofit 2 - 是否有可能获得我发送和接收的JSON的大小?

时间:2017-06-19 11:35:00

标签: android json retrofit

我需要在我的Android应用上构建一个流量监控器,我需要存储我通过改造发送和接收的所有json的大小。使用日志我可以看到它的实际大小,但我还没有找到获取此信息的方法,所以我可以保存它。我无法获得response.raw,因为它已被解析为我的类。有没有办法实现这一目标?

编辑:标记的vadkou答案是最佳答案。

我没有创建新的拦截器,而是传递了lamda表达式:

 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;

    });

3 个答案:

答案 0 :(得分:4)

Retrofit 2在内部使用OkHttp,你可以配置OkHttp,而不必像Vaiden的回答那样通过在构建适配器时添加自定义拦截器来获取原始HTTP响应,如下所示:

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

Interceptor接口允许您为每个请求访问请求正文。

@Override
public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
            // do what you want with request.body().contentLength();
        return chain.proceed(request);
}

答案 1 :(得分:2)

为此,您需要创建自定义的interecptor 请参考以下示例

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

`

现在创建Retrofit对象

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

答案 2 :(得分:1)

您应该尝试访问原始HTTP响应(Get raw HTTP response with Retrofit):

您从Response对象开始。

此对象具有.raw()方法,该方法返回实际HTTP层的响应,  以okhttp3.Response对象的形式。调用.body()会给你一个ResponseBody对象,它封装了原始响应。

您可以致电.contentLength()来获取回复的长度。