我已经在我的项目中成功实施了Retrofit 2,一切都按预期工作。但是一旦互联网不存在,应用程序就会发送多个请求失败的请求。这会创建一堆失败的请求,这些请求会不断生成并导致应用程序被挂起。
我不明白这种行为的原因。
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
答案 0 :(得分:3)
改造不是重试。但OkHttp有这样的功能。看看this article。它默认启用。
您可以在retryOnConnectionFailure上调用OkHttpClient.Builder来停用它。
自Retrofit 2.1.0(OkHttp 3.3.0)以来,这一变化已经出现。
答案 1 :(得分:0)
移动设备未连接到互联网时,请勿反复请求。
在您自己的util类中创建一个常用方法,在调用Internet请求之前检查移动是否连接到Internet,然后发送请求。以下是检查您的手机是否已连接到互联网的常用方法
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;
}