I create an HttpURLConnection
in a Thread or AsyncTask
and it works fine.
But when the Device is in sleep mode, (Display is Off...) then I get an
Exception by open the Connection -> java.net.ConnectException: Connection refused
.
My idea is to wake up the Device with the PowerManager.PARTIAL_WAKE_LOCK
, but that doesn't help.
The other defines from PowerManager for example ACQUIRE_CAUSES_WAKEUP are deprecated.
What can I do to create a successful connection?
The code:
URL url = new URL("myUrl...");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.connect();
int response = urlConnection.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
// to do some work
}