I am developing an Android Wear Watchface that accesses the Internet by sending a MessageAPI request from wearing to my mobile application, where I launch the WearableListenerService service, and in my mobile application I receive a wear request in onMessageReceived and in onMessageReceived I run AsyncTask to retrieve the image from the Internet .
This is working fine.
However, if the phone is inactive for several minutes (black screen not connected to my mac via USB), I get the message "ECONNREFUSED - Connection refused by server" when AsyncTask tries to get the input stream in this line:
InputStream is = (InputStream) new URL(mUrl).getContent();
Here is the Exception:
java.net.ConnectException: failed to connect to dl.dropboxusercontent.com/xx.xx.xxx.xx (port 443): connect failed: ECONNREFUSED (Connection refused) at libcore.io.IoBridge.connect(IoBridge.java:118) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460) at java.net.Socket.connect(Socket.java:838) at com.android.okhttp.internal.Platform.connectSocket(Platform.java:131) at com.android.okhttp.Connection.connect(Connection.java:101) at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294) at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255) at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206) at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345) at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296) at com.android.okhttp.internal.http.HttpURLConnectionImpl.getHeaderField(HttpURLConnectionImpl.java:143) at java.net.URLConnection.getContentType(URLConnection.java:326) at java.net.URLConnection.getContent(URLConnection.java:193) at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getContent(HttpsURLConnectionImpl.java:169) at java.net.URL.getContent(URL.java:455) at wear.webcam.MobileRequestListenerService$MyImageLoaderTask.doInBackground(MobileRequestListenerService.java:479) at wear.webcam.MobileRequestListenerService$MyImageLoaderTask.doInBackground(MobileRequestListenerService.java:451) at android.os.AsyncTask$2.call(AsyncTask.java:288) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:841) Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused) at libcore.io.Posix.connect(Native Method) at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85) at libcore.io.IoBridge.connectErrno(IoBridge.java:131) at libcore.io.IoBridge.connect(IoBridge.java:116) ... 23 more
- My application repeats a minute later, and when the phone is turned on (the screen is on), it works!
- This has nothing to do with the server / url that I call, as I get the same error with different servers.
- This has nothing to do with Wifi, because it happens regardless of whether on Wifi or on a regular network.
- This happens in the PROD / Signed and DEBUG versions.
- I check if the network is available right before the call, and I return "true", so the network is really on!
public boolean isNetworkAvailable() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); // if no network is available networkInfo will be null // otherwise check if we are connected if (networkInfo != null && networkInfo.isConnected()) { return true; } return false; }
It looks like this problem can be solved by acquiring wakelock - however, this has not yet solved the problem.
Any help is appreciated !!!
Android 4.4.4 Sony Xperia Z1; Android Wear 5.0.2.
android android wear
user2996950
source share