androidservicealarmmanagerandroid-webservice

why i am getting SoketTimeOutException in sleep mode of device?


I am calling a web service HTTPS connection from Service class which is called by alarm manager in every 1 min.When i start service it is working fine i am getting response from web service until device gets in sleep mode.when device's screen gets off, on next web service call i am getting this error-

   com.turbomanage.httpclient.HttpRequestException: java.net.SocketTimeoutException: connect timed out

it will continue throw this exception until i wakeup the device.When device get wakeup the webservice start getting response from server and i am not getting exception then. So please anyone tell me what could be the problem.The testing device is Nexsus 5X.


Solution

  • I am calling a web service HTTPS connection from Service class which is called by alarm manager in every 1 min

    That will not work on Android 6.0+, unless your app is on the battery optimization whitelist. Doze mode will stop most of your alarms, and on those cases where you do get control, you may not have Internet access.

    Also, bear in mind that waking up the device every minute to go and perform network I/O will drain the battery, which is why Android 6.0 introduced Doze mode in the first place.