How to handle java.net.UnknownHostException while using retrofit

Anthony

I'm using retrofit to get and post data from the server. However, if my phone loses internet connection in middle of the app then I see error like this:

05-10 08:12:05.559  29369-29400/? D/Retrofit﹕ java.net.UnknownHostException: Unable to resolve host "my.server.com": No address associated with hostname
            at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
            at java.net.InetAddress.getAllByName(InetAddress.java:214)

I would like to handle this error gracefully. I would like to catch the exception and show a toast message like "No internet connection".

I'm trying code like this but I get an error that: java.net.UnknownHostException is never thrown in the try block

  try {
      isTokenValid = MyManager.INSTANCE.getService().validate();
  }
  catch (RetrofitError cause) {
      Response r = cause.getResponse();

      if (r != null && r.getStatus() == 403) {
           isTokenValid = false;
      }
  }
  catch (UnknownHostException exception) {
      Toast.makeText(getBaseContext(), "No internet connection", Toast.LENGTH_SHORT).show();
  }
user2511882

I think you need to have a retrofit exception in the catch.

catch (RetrofitError error) {
    methodToDeterminErrorType(error);
}

The RetroFitError is a generic runtime exception. Once it hits the catch block you can actually verify what kind of a error was actually thrown by retrofit. Retrofit has a method isNetworkError() which is what you are probably looking for. So you can basically do something like this:

methodToDetermineErrorType(RetroFitError error) {
if (error.isNetworkType()) {
    // show a toast
   }
}

Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Error java.net.UnknownHostException while connecting Cassandra cluster

java.net.UnknownHostException cannot be cast to retrofit2.adapter.rxjava.HttpException with RxJava

Using Retrofit How to handle this json response form?

How to handle 204 response in Retrofit using Dart?

How to handle null while comparing maps using streams - Java?

java.net.UnknownHostException: postgres

java.net.UnknownHostException on Docker

how to get response in retrofit while using rxAndroid?

How to handle pagination in retrofit

How to handle NULL value in JSON Object using retrofit

How to handle errors from parallel web requests using Retrofit + RxJava?

How to handle 204 response in Retrofit using Kotlin Coroutines?

How to handle errors with exception filter while deploying ASP .NET Core 5 application using Linux Docker containers?

java.net.UnknownHostException error in InetAddress

java.net.UnknownHostException: Name or service not known

java.net.UnknownHostException on file:// method

java.net.UnknownHostException harvest Dspace collection

java.net.UnknownHostException spring ,zuul ,eureka

Websphere 8.5 - java.net.UnknownHostException

Error: -copyFromLocal: java.net.UnknownHostException

Retrofit - How to error handle the NumberFormatException in retrofit?

Sqoop: Exception ERROR tool.ImportTool: Import failed: java.net.UnknownHostException: host: host: unknown error while importing

How to handle Dynamic JSON in Retrofit?

How to handle errors dynamically in Retrofit

How to use retrofit as a singleton while using gson convertor?

How to specify a different name for json key while using Retrofit + Moshi

How to change the RequestInterface dynamically while parsing limited JSON using Retrofit?

How to get the request url while using moshi 'deferred' with retrofit 2.0

How to handle java.net.ConnectException efficiently