Alan
Alan

Reputation: 589

Is there a "standard" approach for handling HttpConnection errors in Android (e.g. no Internet)

I have a method which calls an endpoint via GET request. I was sure, checking for internet before the operation would be the best approach, BUT:

1) What if the internet connection is lost during the operation?
2) Pinging Google DNS to check for a connection, what if the internet provider blocks pings to it?

Is it better to connect to the endpoint, get the data from there and if it fails (No internet - UnknownHostException) handle it on demand or not?

Upvotes: 0

Views: 27

Answers (1)

a_local_nobody
a_local_nobody

Reputation: 8191

As I said in the comments, connectivity is never a certain fact and the possibility of losing connection OR your request failing for any number of reasons is always there. To properly handle service calls (in my opinion at least) always consider the possibility of either your user losing internet or your service call not returning at all, as well as any other error responses which you could receive from the actual request. Handle the response per call and consider the possibility of your request entirely failing or timing out as well.

For HttpURLConnection, you'd use getErrorStream as one of the methods to determine any errors.

enter image description here

Upvotes: 1

Related Questions