What Can I do to manage cells that says they have Internet but that can't reach any site

180 Views Asked by At

I am experimenting errors in my app, when cell is telling that it is connected to internet, but when you try to connect google play or browser, you can see there is no internet.

It can happen when user is at the limit of Wifi Range, or when there is a very bad 3g signal, or when the device has not so good hardware ( I have a lot of problems with Samsung Advance with Android 4.1.2)

The consequence is I have a lot of errors like:

ECONNRESET (Connection reset by peer)

or

java.net.UnknownHostException: Unable to resolve host "mysite.com": No address associated with hostname
   at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
   at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
   at java.net.InetAddress.getAllByName(InetAddress.java:214)
   at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
   at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
   at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
   at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
   at com.myapp.action.CapturaDAO.uploadCapturas(CapturaDAO.java:70)
   at com.myapp.action.SyncData.doInBackground(SyncData.java:148)
   at com.myapp.action.SyncData.doInBackground(SyncData.java:1)
   at android.os.AsyncTask$2.call(AsyncTask.java:287)
   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
   at java.util.concurrent.FutureTask.run(FutureTask.java:137)
   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
   at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
   at libcore.io.Posix.getaddrinfo(Posix.java)
   at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
   at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
   at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
   at java.net.InetAddress.getAllByName(InetAddress.java:214)
   at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
   at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
   at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
   at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
   at com.myapp.action.CapturaDAO.uploadCapturas(CapturaDAO.java:70)
   at com.myapp.action.SyncData.doInBackground(SyncData.java:148)
   at com.myapp.action.SyncData.doInBackground(SyncData.java:1)
   at android.os.AsyncTask$2.call(AsyncTask.java:287)
   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
   at java.util.concurrent.FutureTask.run(FutureTask.java:137)
   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
   at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.ErrnoException: getaddrinfo failed: ENETUNREACH (Network is unreachable)
   at libcore.io.Posix.getaddrinfo(Posix.java)
   at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
   at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
   at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
   at java.net.InetAddress.getAllByName(InetAddress.java:214)
   at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
   at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
   at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
   at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
   at com.myapp.action.CapturaDAO.uploadCapturas(CapturaDAO.java:70)
   at com.myapp.action.SyncData.doInBackground(SyncData.java:148)
   at com.myapp.action.SyncData.doInBackground(SyncData.java:1)
   at android.os.AsyncTask$2.call(AsyncTask.java:287)
   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
   at java.util.concurrent.FutureTask.run(FutureTask.java:137)
   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
   at java.lang.Thread.run(Thread.java:856)

When loading, it lasts a lot.

My first question is: Is it consuming high battery while trying to connect host???

My real question: is it posible to manage this case? Is there a way to detect there is no internet while system says there is?

EDIT:

I'm checking the internet connection with that method :

public static boolean isOnline(Context ctx) {
    ConnectivityManager cm = (ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}
1

There are 1 best solutions below

0
On

My first question is: Is it consuming high battery while trying to connect host???

Battery consumption should be normal as it is using the wi-fi or mobile radio.

My real question: is it possible to manage this case? Is there a way to detect there is no internet while system says there is?

You can add a timeout of say 30 sec with your request using URLConnection's setConnectionTimeput() method. In case of connection time out you can notify the user about weak connection.