2

I have successfully implemented Retrofit 2 in my project and everything is working as expected. But once internet is not present, the the app is sending multiple requests for the failed request. This creates a stack of failed requests that keep getting generated and results in app getting hanged.

I dont understand the cause for such behaviour.

02-01 16:16:08.812 27970-28054/? D/OkHttp: --> GET https://api.newsapi.in/v1/stories/category/india?nobody=true&skip=0&limit=10 http/1.1
02-01 16:16:08.812 27970-28054/? D/OkHttp: --> END GET
02-01 16:16:08.812 27970-28054/? D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "api.newsapi.in": No address associated with hostname
02-01 16:16:08.912 27970-28054/? D/OkHttp: --> GET https://api.newsapi.in/v1/stories/category/international?nobody=true&skip=0&limit=10 http/1.1
02-01 16:16:08.912 27970-28054/? D/OkHttp: --> END GET
02-01 16:16:08.912 27970-28054/? D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "api.newsapi.in": No address associated with hostname
02-01 16:16:08.922 27970-27970/? E/NewsListFragment: java.net.UnknownHostException: Unable to resolve host "api.newsapi.in": No address associated with hostname
02-01 16:16:08.922 27970-27970/? E/NewsListFragment: java.net.UnknownHostException: Unable to resolve host "api.newsapi.in": No address associated with hostname
02-01 16:16:08.942 27970-27979/? I/dalvikvm: Jit: resizing JitTable from 4096 to 8192
02-01 16:16:09.042 27970-28054/? D/OkHttp: --> GET https://api.newsapi.in/v1/stories/category/india?nobody=true&skip=0&limit=10 http/1.1
02-01 16:16:09.042 27970-28054/? D/OkHttp: --> END GET
02-01 16:16:09.042 27970-28054/? D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "api.newsapi.in": No address associated with hostname
02-01 16:16:09.142 27970-28054/? D/OkHttp: --> GET https://api.newsapi.in/v1/stories/category/international?nobody=true&skip=0&limit=10 http/1.1
02-01 16:16:09.142 27970-28054/? D/OkHttp: --> END GET
02-01 16:16:09.142 27970-28054/? D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "api.newsapi.in": No address associated with hostname
02-01 16:16:09.152 27970-27970/? E/NewsListFragment: java.net.UnknownHostException: Unable to resolve host "api.newsapi.in": No address associated with hostname
02-01 16:16:09.162 27970-27970/? E/NewsListFragment: java.net.UnknownHostException: Unable to resolve host "api.newsapi.in": No address associated with hostname
02-01 16:16:09.282 27970-28054/? D/OkHttp: --> GET https://api.newsapi.in/v1/stories/category/india?nobody=true&skip=0&limit=10 http/1.1
02-01 16:16:09.282 27970-28054/? D/OkHttp: --> END GET
02-01 16:16:09.282 27970-28054/? D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "api.newsapi.in": No address associated with hostname
02-01 16:16:09.402 27970-28054/? D/OkHttp: --> GET https://api.newsapi.in/v1/stories/category/international?nobody=true&skip=0&limit=10 http/1.1
02-01 16:16:09.402 27970-28054/? D/OkHttp: --> END GET
02-01 16:16:09.402 27970-28054/? D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "api.newsapi.in": No address associated with hostname
02-01 16:16:09.412 27970-27970/? E/NewsListFragment: java.net.UnknownHostException: Unable to resolve host "api.newsapi.in": No address associated with hostname
02-01 16:16:09.432 27970-27970/? D/dalvikvm: GC_FOR_ALLOC freed 2592K, 13% free 21331K/24320K, paused 27ms, total 27ms
02-01 16:16:09.442 27970-27970/? E/NewsListFragment: java.net.UnknownHostException: Unable to resolve host "api.newsapi.in": No address associated with hostname
02-01 16:16:09.552 27970-28054/? D/OkHttp: --> GET https://api.newsapi.in/v1/stories/category/india?nobody=true&skip=0&limit=10 http/1.1
02-01 16:16:09.552 27970-28054/? D/OkHttp: --> END GET
02-01 16:16:09.552 27970-28054/? D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "api.newsapi.in": No address associated with hostname
02-01 16:16:09.662 27970-28054/? D/OkHttp: --> GET https://api.newsapi.in/v1/stories/category/international?nobody=true&skip=0&limit=10 http/1.1
02-01 16:16:09.662 27970-28054/? D/OkHttp: --> END GET
02-01 16:16:09.662 27970-28054/? D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "api.newsapi.in": No address associated with hostname
02-01 16:16:09.662 27970-27970/? E/NewsListFragment: java.net.UnknownHostException: Unable to resolve host "api.newsapi.in": No address associated with hostname
02-01 16:16:09.672 27970-27970/? E/NewsListFragment: java.net.UnknownHostException: Unable to resolve host "api.newsapi.in": No address associated with hostname

2 Answers2

3

Retrofit is not retrying. However OkHttp has such feature. Look at this article. It is enabled by default.

You can disable it by calling retryOnConnectionFailure on OkHttpClient.Builder.

This change is present since Retrofit 2.1.0 (OkHttp 3.3.0).

R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
0

Do not request over and over again when mobile is not connected to internet.

create a common method in util class of your own where you check before calling internet request whether mobile is connected to internet or not and then send request. below is the common method to check whether your mobile is connected to internet or not

public static boolean hasNetworkConnection(Context context) {
    if (context == null) return false;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
    //Was causing crash due to required permission for access network state
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null) {
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            return activeNetwork.isConnected();
        else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            return activeNetwork.isConnected();
    }
    return false;
}
Usman Ghauri
  • 931
  • 1
  • 7
  • 26