Search code examples
androidretrofitandroidhttpclient

Android What happens during HTTP transaction if Device sleeps?


I couldn't produce this example which iam about to explain, so i post this here to give me more info about it.

Lets say that we want to make an http request to receive a JSON object from a server. We hold no wakelocks nor we make our app a foreground. We make the connections and we wait for our response from server.

What if the device goes to sleep at that very moment (waiting for the JSON object). Do we get the object when the device awakes or we get a timeout exception?

NOTE

Do not involve doze mode in this example. Just that device goes to sleep. Thank you


Solution

  • When an Android is kept idle, the device locks and then it goes to deep sleep mode. In deep sleep mode the Android system, closes the existing network connections like TCP or UDP. If your app is connected to a server, it loses connection to the server and tries to reconnect based on the reconnect attempt methods configured for the client. But if your app is the server, all the client will lose connection to the server, and you have to start the socket again in server and try to connect once again from the clients.

    From https://stackoverflow.com/a/33366487/806328

    retrofit just "listens" for network connection, when it loses it - connection time out error after specified time