How to deal with network problems

One of my applications uses many HTTP requests to communicate with its internal content. To fulfill these requests, I use two different implementations:

  • Volley library for most cases
  • The combination of AsyncTask and DefaultHttpClient for several cases

In most cases, everything works well. But sometimes I have a bunch of network exceptions raised and shown in Crashlytics:

  • java.net.UnknownHostException: Unable to resolve host "mydomain.com": No address associated with hostname Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)

  • com.android.volley.ServerError at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:175) at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)

With a little research, I found that this should happen when the device has really bad 3g / 4g or beyond a VPN / subnet, so it cannot get to my site.

How can I make sure the device is really connected to the Internet? I actually execute these requests only if this function returns true:

 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; } 

Or should I just let it go and assume it's normal to receive up to several hundred of these alerts per month?

+5
source share
3 answers

To find out if you have internet, you need to do something more than check if you have a network connection. Because if you are connected to a slow network, portable portal or VPN, you have a connection to the network, but there is no real Internet or the Internet used.

That is why you still need to check if you have internet / used internet before you make an HTTP request by adding a simple ping to the server or Google (because Google is 99.99% of the time). You can also do this periodically or when you catch the first exception you need.

 public Boolean isInternet() { try { Process process = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com"); int returnVal = process.waitFor(); boolean reachable = (returnVal==0); return reachable } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } 

You can also use a different method, but the general idea is the same as you know, almost always online, and check if you can achieve it.

However, continue to handle exceptions, because there is no way to always catch it, and having a backup way of handling things cannot damage

0
source

How can I make sure the device is really connected to the Internet?

You have learned. This is what they say about mistakes.

How can I make sure the device is really connected to the Internet? I actually execute these requests only if this function returns true:

I am not a supporter of this kind of thing:

  • If he checks the exact same things as the actual request, he (i) duplicates the work and (ii) is not executed at the same time
  • If he doesn’t check the exact same things as the actual request, you will most likely receive both false positives and false negatives.
  • in all cases, it is an attempt to predict the future. It may fail, and the request will succeed or succeed now, and the request will fail.
  • you still need to write error handling code for the request.

In general, the best way to check if a resource is available is to simply try to use it in the usual way and deal with any adverse effects as they arise.

Or should I just let it go and assume it's normal to receive up to several hundred of these alerts per month?

I would not say that ServerError is normal. Perhaps the server managed to send this to you through a fully functioning network, so this may be a server or client error that you should investigate. Network failures, on the other hand, are β€œnormal” in any topology and technology.

0
source

Yes, you should check the availability of the Internet before making an HTTP request, this will save you a lot.

The code you owe is very good and much better than pinging any domain.

But in your case, your condition will not be sufficient, because the adapter is actually connected, but you will not be able to connect to your server due to a poor connection.

Therefore, you should still handle the timeout and IO exceptions that may occur during the execution of your requests.

- Set a timeout for your Httpclient - and I think volleyball has a timeout by default.

-1
source

Source: https://habr.com/ru/post/1207014/


All Articles