hgoebl / DavidWebb

Lightweight Java HTTP-Client for calling JSON REST-Services (especially for Android)
https://hgoebl.github.io/DavidWebb/
MIT License
127 stars 41 forks source link

How to handle error if there is no internet connection #27

Closed hatimmakki closed 6 years ago

hatimmakki commented 7 years ago

Hi How to handle the exception that raise when no internet connection? thanks

hgoebl commented 7 years ago

A WebbException will be thrown. From there, you can get the Response object via getResponse(). When there is no Internet connection, you typically will read 0 from getStatusCode(). Internally often an exception telling name resolution didn't work, but that is not mandatory, e.g. if you try to reach your server via IP-address, then another error will be wrapped inside WebbException.

How you handle this kind of error is up to you. DavidWebb has a feature of doing retries, but I haven't tried this in case there are connection errors.

If you want to avoid running into errors caused by lack of internet connectivity, you could check that before doing a request. I suppose this could be a bit complicated, because you might deal with WiFi and mobile connectivity. And you might have to add additional privileges before being able to call network status methods.