9

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!

user2547667
  • 221
  • 1
  • 2
  • 7
  • Does this answer your question? [Retrofit 2 - Is it possible to get the size of a JSON that I'm sending and receiving?](https://stackoverflow.com/questions/44629502/retrofit-2-is-it-possible-to-get-the-size-of-a-json-that-im-sending-and-recei) – darwin Apr 13 '20 at 05:18

1 Answers1

1

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

Yrii Borodkin
  • 674
  • 5
  • 7