Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit: How to get the size of Request and Response?

I am developing an android app and using retrofit to send requests and get responses. Like this:

public interface Service {
    @POST("/GetInfo")
    InfoResponse getInfo(@Body InfoRequest request);
}

I want to know how much data it will be used when doing these requests.

I find if I set the LogLevel for retrofit, it could print out the size in log.

For request:

---> HTTP POST http://....
Content-Type: application/json; charset=UTF-8
Content-Length: 516
---> END HTTP (522-byte body)

For response:

<--- HTTP 200  http://....
:HTTP/1.1 200 OK 
Content-Type: application/json 
<--- END HTTP (420394-byte body)

This 522-byte and 420394-byte information what I want. Is there any way to get it directly without getting the log and parsing it?

If I have to use this log in order to calculate the size, any good way to get the size from log and then show on android?

Thanks!

like image 716
user2547667 Avatar asked May 04 '16 21:05

user2547667


1 Answers

You can add network intecepter in you retrofit builder and check size using responseBody.source().apply{request(Long.MAX_VALUE)}.buffer.size for response and request.body.contentLength() for request

like image 85
Yrii Borodkin Avatar answered Nov 08 '22 16:11

Yrii Borodkin