Search code examples
androidandroid-asynctaskandroid-broadcastreceiver

Android AsyncTask from Service (started from receiver) unable to resolve host


I have an AsyncTask, connecting to my server.

When I run it from Activity - everything works good.

But when it is started from BroadcastReceiver inside onReceive() and application is in background - it always throws Exception:
Unable to resolve host *myHostHere* No address associated with hostname

When application is in foreground - everything is also good.

Where did I make mistake?

1. onReceive() code:

@Override
    public void onReceive(final Context context, Intent intent) {
        ExecutorService service = Executors.newSingleThreadExecutor();
        service.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    MonitoringHelper.getInstance(context).sendData();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

2. In sendData() I run AsyncTask with:

new SendMessageAsync(context, this).execute(json);

3. doInBackground():

@Override
    protected Boolean doInBackground(String ... params) {
        HttpPost httpPost = new HttpPost("http://" + serverAddress.replace("http://", "") + "/mobile/message");
        httpPost.setHeader("Content-type", "application/json");

        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            httpPost.setEntity(new StringEntity(params[0]));
            response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }

4. Logcat:

09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err: java.net.UnknownHostException: Unable to resolve host "myHostHere": No address associated with hostname
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:457)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:215)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:369)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at com.hys.behappy.utils.SendMessageAsync.doInBackground(SendMessageAsync.java:65)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at com.hys.behappy.utils.SendMessageAsync.doInBackground(SendMessageAsync.java:27)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at java.lang.Thread.run(Thread.java:818)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err: Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at libcore.io.Posix.android_getaddrinfo(Native Method)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
09-14 10:58:05.684 16024-25007/com.hys.behappy W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:438)

PS. Permissions in manifest

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

PPS. Moved AsyncTask to IntentService.
Result is the same.

PPPS. Seems to be great device-specific issue - it doesn't work on Xiaomi Redmi Note 3 Pro, but works like charm on LGE Nexus.
Any advices?


Solution

  • I'm not sure if this is still active but I've had same problem and managed to find solution.

    Xiaomi by default restricts all background activites when possible and you can actually disable that for all apps or just for some specific apps.

    What restricting actually does is that it restricts internet connection for your app when it's backgrounded so you get UnknownHostException because it actually has no internet connection.

    So, to fix this just go to Settings -> "Battery & performance" -> "Manage apps battery usage" and turn it off completely for all apps or click on "Choose apps" and turn it off only for your app.

    Hope it helps!