0

I'm declaring an object like this:

Response getMessagesResponse;
        if (page == 0) {
            getMessagesResponse = myRequest.newCall(getMessagesRequest).execute();
            System.out.println("response "+getMessagesResponse.body().string());

The code above will print an empty string, no matter how times I run it. Now, if I assign and call the object at the same line, like this:

if (page == 0) {
            Response a = sisgradRequest.newCall(getMessagesRequest).execute();
            System.out.println("response "+a.body().string());

it will work. Why? I've always did things like this in java and had no problem. The newCall method here is from the OkHttp3 java library.

UPDATE:

before System.out.println() in the first code, I had this line:

        this.magicalNumber = getMagicalNumber(getMessagesResponse.body().string());

turns out that if I put System.out.println() before it, I get the print. If i put after it, I don't get it. Why? Maybe because jSoup inside getMagicalNumber processes the data and then erases it from the main object?

Gatonito
  • 1,662
  • 5
  • 26
  • 55
  • 5
    Perhaps `myRequest` isn't returning data, but `sisgradRequest` is? – azurefrog Aug 17 '16 at 20:40
  • More context might be helpful too. – lucasvw Aug 17 '16 at 20:42
  • @azurefrog they're calls for the same request, and also I checked the header content-lenght and its huge – Gatonito Aug 17 '16 at 20:42
  • @lucasvw hmmm, they're calls for the same request, inside a method that's called by main(). I was trying to print the object outside the if but it won't work. I tried then inside if, won't work too. However, System.out.println(getMessagesRequest.request()) will print data related to the call :c – Gatonito Aug 17 '16 at 20:44
  • 4
    Something else is up. Have you tried debugging? – Taylor Aug 17 '16 at 20:44
  • please see my update – Gatonito Aug 17 '16 at 20:48
  • 1
    Possibly it's a stream that's being consumed somewhere in the internals. – Taylor Aug 17 '16 at 20:49
  • perhaps getMagicalNumber() is never returning, or executes an exit() somewhere, or is throwing an exception? Run it in a debugger! – FredK Aug 17 '16 at 20:51

1 Answers1

1

As @Taylor commented, the ResponseBody is a stream which can only be consumed once. From the documentation,

The response body can be consumed only once.

This class may be used to stream very large responses. For example, it is possible to use this class to read a response that is larger than the entire memory allocated to the current process. It can even stream a response larger than the total storage on the current device, which is a common requirement for video streaming applications.

Because this class does not buffer the full response in memory, the application may not re-read the bytes of the response. Use this one shot to read the entire response into memory with bytes() or string(). Or stream the response with either source(), byteStream(), or charStream().

Also see this answer: https://stackoverflow.com/a/32307866/1371329

Community
  • 1
  • 1
jaco0646
  • 15,303
  • 7
  • 59
  • 83