我正在开发应用程序。我正在发送经过改造的API请求。如果没有互联网,我想存储此请求,并在互联网连接后立即再次发送。我该怎么办?
答案 0 :(得分:-1)
您必须将请求数据保存在DB或SharedPreferences中的onError回调中,并在检测到网络状态更改时将其恢复并再次发送。
用于检测网络状态是否已更改,如下所示:
<div class="sku tooltipp"><span>Hover over or touch me to see the full version of this string. It looks like replaced by tooltip.</span></div>
<div class="sku tooltipp"><span>Short</span></div>
并将这些行添加到清单中的清单中,如下所示:
item
此外,您可以通过调用此功能来检查网络状态更改时互联网是否可用:
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
final ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isAvailable() || mobile.isAvailable()) {
// Network is available.
// Also check internet is available or not...
}
}
}