2013-12-16 58 views
0

async-下载如何处理,如果从断开用户简历的Android安卓 Android系统的AsyncTask文件下载Java IO 我上一个异步下载工作处理重新连接在机器人

我简单地使用I/O流下载

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. 
    } 

并试图捕捉,如果有异常提醒消息。问题是,如果用户连接网络,我如何恢复下载?或者说,如果重新连接,只需自动执行下载任务?谢谢

+0

我相信你需要实现一个DownloadManager类,在这个类中你可以跟踪当前的下载和状态。您还需要一个BroadcastReceiver来了解用户何时重新联机。从广播接收器中,您通知下载管理器恢复正在进行的下载。这可能不是适合你的解决方案,但这是我现在可以想到的。 –

+0

感谢您的建议。下载管理器很好用,但我需要制作一个下载队列 – user1871516

回答

1

这有很多部分。首先,我建议使用droidQuery来处理您的网络任务。它是高度可配置的,已经可以处理错误,超时问题等。为了让您一开始,尝试这样的事:

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); 
    } 
})); 

requeue方法检查网络状态。如果网络启动,请求再次尝试。如果不可用,当网络可用时,它将排队等待运行。此队列将用于:

public static List<AjaxOptions> queue = new ArrayList<AjaxOptions>(); 

所以这种方法看起来是这样的:

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); 
     } 
    } 
} 

最后,为了确保排队的请求被称为当网络可用时,你需要使用一个BroadcastReceiver来聆听网络中的变化。这方面的一个很好的例子是NetWatcher,但你的onReceive方法看上去就像这样:

@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(); 
      } 
     } 
    } 
} 

如果你想queue变量是private,你可以注册代码广播接收器(而不是在清单) ,并使其成为您的活动的内部课程。最后,为确保您的BroadcastReceiver按预期工作,你需要以下权限:

<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> 

还要保证网络可用时,不要忘了INTERNET许可。