android异步下载中的句柄重新连接
在android中,如果用户从断开连接恢复,如何处理 android异步任务文件下载java io 我正在使用异步下载程序 我只是使用I/O流下载android异步下载中的句柄重新连接,android,asynchronous,io,android-asynctask,download,Android,Asynchronous,Io,Android Asynctask,Download,在android中,如果用户从断开连接恢复,如何处理 android异步任务文件下载java io 我正在使用异步下载程序 我只是使用I/O流下载 try { URL url = new URL(myurl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000 /* milliseconds */);
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
Log.d(DEBUG_TAG, "The response is: " + response);
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(is, len);
return contentAsString;
// Makes sure that the InputStream is closed after the app is
// finished using it.
}
以及一个try-catch,用于在出现异常时提醒消息。问题是,如果用户重新连接网络,我如何恢复下载?或者说,如果重新连接,只需自动执行下载任务?谢谢这有很多部分。首先,我建议使用来处理网络任务。它是高度可配置的,并且已经可以处理错误、超时问题等。要开始,请尝试以下方法:
Integer[] statusCodes = new Integer[]{480,522};//request and connection timeout error codes
$.ajax(new AjaxOptions().url(myURL).timeout(1000).dataType("text").statusCode(statusCodes, new Function() {
@Override
public void invoke($ d, Object... args) {
//queue task to run again.
int statusCode = (Integer) args[0];
Log.e("Ajax", "Timeout (Error code " + statusCode + ").");
requeue((AjaxOptions) args[1]);
}
}).success(new Function() {
@Override
public void invoke($ d, Object... args) {
//since dataType is text, the response is a String
String response = (String) args[0];
Log.i("Ajax", "Response String: " + response);
}
}).error(new Function() {
@Override
public void invoke($ d, Object... args) {
//show error message
AjaxError error = (AjaxError) args[0];
Log.e("Ajax", "Error " + error.status + ": " + error.reason);
}
}));
private void requeue(AjaxOptions options)
{
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info == null)
cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (info == null)
cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (info != null && info.isConnectedOrConnecting())
$.ajax(options);
else {
synchronized(queue) {
queue.add(options);
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
//here, check that the network connection is available. If yes, start your service. If not, stop your service.
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
if (info.isConnected()) {
synchronized(myActivity.queue) {
for (AjaxOptions options : myActivity.queue) {
$.ajax(options);
}
myActivity.queue.clear();
}
}
}
}
您的requeue
方法将检查网络状态。如果网络已启动,将再次尝试请求。如果它不可用,它将在网络可用时排队运行。此队列将用于:
public static List<AjaxOptions> queue = new ArrayList<AjaxOptions>();
最后,为了确保在网络可用时调用排队请求,您需要使用侦听网络中的更改。一个很好的例子是,但是您的onReceive
方法看起来更像这样:
Integer[] statusCodes = new Integer[]{480,522};//request and connection timeout error codes
$.ajax(new AjaxOptions().url(myURL).timeout(1000).dataType("text").statusCode(statusCodes, new Function() {
@Override
public void invoke($ d, Object... args) {
//queue task to run again.
int statusCode = (Integer) args[0];
Log.e("Ajax", "Timeout (Error code " + statusCode + ").");
requeue((AjaxOptions) args[1]);
}
}).success(new Function() {
@Override
public void invoke($ d, Object... args) {
//since dataType is text, the response is a String
String response = (String) args[0];
Log.i("Ajax", "Response String: " + response);
}
}).error(new Function() {
@Override
public void invoke($ d, Object... args) {
//show error message
AjaxError error = (AjaxError) args[0];
Log.e("Ajax", "Error " + error.status + ": " + error.reason);
}
}));
private void requeue(AjaxOptions options)
{
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info == null)
cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (info == null)
cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (info != null && info.isConnectedOrConnecting())
$.ajax(options);
else {
synchronized(queue) {
queue.add(options);
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
//here, check that the network connection is available. If yes, start your service. If not, stop your service.
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
if (info.isConnected()) {
synchronized(myActivity.queue) {
for (AjaxOptions options : myActivity.queue) {
$.ajax(options);
}
myActivity.queue.clear();
}
}
}
}
如果希望队列
变量为私有
,可以在代码中注册广播接收器(而不是在清单中),并将其作为活动的内部类。最后,为确保您的BroadcastReceiver
按预期工作,您需要以下权限:
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
另外,为了确保internet可用,请不要忘记
internet
权限。我认为您需要实现一个DownloadManager类,在该类中您可以跟踪当前下载和状态。此外,您还需要一个广播接收器来了解用户何时恢复在线。从广播接收器,通知下载管理器恢复正在进行的下载。这对你来说可能不是一个合适的解决方案,但这是我现在可以考虑的。谢谢你的建议。下载管理器很好用,但我需要建立一个下载队列