我正在尝试从互联网上下载文件。我可以一个接一个地下载多个文件。当一个donload完成时,我通过构建新连接并重复第一次下载的所有步骤来启动下一个。所以我认为我可能同时打开多个连接。
问题是,在加载3或4个文件后,下载的文件不会被下载。 我希望有更好的方法可以在创建新连接之前使用现有连接进行进一步下载或破坏现有连接。这是我的下载程序类的代码:
public class Downloader {
private OnDownloadCompleteListener onDownloadCompleteListener;
public void startDownload(String url){
new DownloadTask().execute(url);
}
private class DownloadTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
try { return downloadURL(params[0]); } catch (IOException e) { return "cantDownload"; }
}
@Override
protected void onPostExecute(String result) {
onDownloadCompleteListener.onDownloadComplete(result);
}
}
private String downloadURL(String targetURL) throws IOException {
InputStream is = null;
URL url;
try{
url = new URL(targetURL);
Proxy proxy = new Proxy(Proxy.Type.HTTP , new InetSocketAddress( "192.168.100.13" , 8080));
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
conn.setReadTimeout(10000 );
conn.setConnectTimeout(15000 );
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
int response = conn.getResponseCode();
String line = null;
StringBuffer tmp = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while((line = reader.readLine()) != null){ tmp.append(line); }
line = String.valueOf(tmp);
return line ;
} finally {
if (is != null) {
is.close();
}
}
}
public void setOnDownloadCompleteListener(OnDownloadCompleteListener onDownloadCompleteListener){
this.onDownloadCompleteListener = onDownloadCompleteListener;
}
public interface OnDownloadCompleteListener{
public void onDownloadComplete(String downloadedHTML);
}
}
要开始下载,我从主类调用方法startDownload(url)。 当这个下载完成后,我再次调用startDownload方法,所以基本上一切都重复了。 有什么更好的方法呢?
答案 0 :(得分:0)
我之前没有使用过这门课程,但按照说明我觉得这样的事情会奏效。基本上,不同之处在于startDownload同时处理所有网址。有关相关示例,请参阅AsyncTask文档。
public void startDownload(String... urls) {
new DownloadTask().execute(urls)
}
private class DownloadTask extends AsyncTask<String,Void,String[]> {
protected String[] doInBackground(String... params) {
try { return downloadURL(params); } catch (IOException e) { return "cantDownload"; }
}
private String[] downloadURL(String... targetURLs) throws IOException {
String[] lines = new String[targetURLS.length];
for(int i = 0;i<targetURLs.length;i++) {
try{
url = new URL(targetURL);
Proxy proxy = new Proxy(Proxy.Type.HTTP , new InetSocketAddress( "192.168.100.13" , 8080));
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
conn.setReadTimeout(10000 );
conn.setConnectTimeout(15000 );
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
int response = conn.getResponseCode();
String line = null;
StringBuffer tmp = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while((line = reader.readLine()) != null){ tmp.append(line); }
line = String.valueOf(tmp);
lines[i] = line;
} finally {
if (is != null) {
is.close();
}
}
return lines;
}
}
}