android 2.1 AsyncTask文件下载不适用于多线程,调用单个实例时工作正常

android 2.1 AsyncTask文件下载不适用于多线程,调用单个实例时工作正常,android,multithreading,android-asynctask,Android,Multithreading,Android Asynctask,我正在使用Android2.1,api级别7,并尝试从LAN服务器实现异步文件下载 为此,我尝试实现AsyncTask。当我尝试调用单个线程时,它可以找到,但当调用多个线程时,它只是停止这两个线程 /* AsyncTask class*/ class DownloadFileAsync extends AsyncTask<String, String, String> { @Override

我正在使用Android2.1,api级别7,并尝试从LAN服务器实现异步文件下载 为此,我尝试实现AsyncTask。当我尝试调用单个线程时,它可以找到,但当调用多个线程时,它只是停止这两个线程

  /* AsyncTask class*/  
          class DownloadFileAsync extends AsyncTask<String, String, String> {
                @Override
                protected void onPreExecute() {        
                    super.onPreExecute();       
                }

                @Override
                protected String doInBackground(String... aurl) {

                    int count;

                    try {
                        URL url = new URL(aurl[0]);
                        Log.i("count","in");
                        URLConnection conexion = url.openConnection();
                        Log.i("count","in1");
                        conexion.connect();
                        Log.i("count","in2");
                        File root = android.os.Environment.getExternalStorageDirectory();
                        Log.i("count","in3");
                        int lenghtOfFile = conexion.getContentLength();
                        Log.i("count","in4");        
                        BufferedInputStream input = new BufferedInputStream(url.openStream());
                        Log.i("count","in5");
                        OutputStream output = new FileOutputStream(root.getAbsolutePath() + "/video" +aurl[1] +".mp4");
                        byte data[] = new byte[1024];
                        long total = 0;
                        Log.i("count","in6");
                        while ((count = input.read(data)) != -1) {
                            //Log.i("count","in7");
                            total += count;
                            Log.i("count",aurl[1]);
                            publishProgress(""+(int)((total*100)/lenghtOfFile));
                            output.write(data, 0, count);
                        }
                        output.flush();
                        output.close();
                        input.close();          
                    } catch (Exception e) {}
                    return null;
                }

                protected void onProgressUpdate(String... progress) {        
                    Log.i("progress",progress[0]);
                }

                @Override
                protected void onPostExecute(String unused) {        
                    Log.i("process","end");       
                }
            } 

  /*main method call*/
private void startDownload() {
        Log.v("count","out");
        String url = lanurl+"titanic/video"+1+"_en.m4v";
        new DownloadFileAsync().execute(url,"1");

        url = lanurl+"titanic/video"+2+"_en.m4v";
        new DownloadFileAsync().execute(url,"2");

    }
output :
download both file in sd card
but no file downloading properly 
/*异步任务类*/
类DownloadFileAsync扩展AsyncTask{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的字符串背景(字符串…aurl){
整数计数;
试一试{
URL=新URL(aurl[0]);
Log.i(“count”、“in”);
URLConnection conexion=url.openConnection();
Log.i(“count”、“in1”);
conexion.connect();
对数i(“计数”、“平方英寸”);
文件根=android.os.Environment.getExternalStorageDirectory();
Log.i(“计数”、“in3”);
int lenghtOfFile=conexion.getContentLength();
Log.i(“计数”、“in4”);
BufferedInputStream输入=新的BufferedInputStream(url.openStream());
Log.i(“计数”、“in5”);
OutputStream输出=新文件OutputStream(root.getAbsolutePath()+“/video”+aurl[1]+”.mp4”);
字节数据[]=新字节[1024];
长总计=0;
Log.i(“计数”、“in6”);
而((计数=输入。读取(数据))!=-1){
//Log.i(“计数”、“in7”);
总数+=计数;
Log.i(“计数”,aurl[1]);
出版进度(“+(int)((总计*100)/长度文档));
输出.写入(数据,0,计数);
}
output.flush();
output.close();
input.close();
}捕获(例外e){}
返回null;
}
受保护的void onProgressUpdate(字符串…进度){
Log.i(“进度”,进度[0]);
}
@凌驾
受保护的void onPostExecute(字符串未使用){
日志i(“过程”、“结束”);
}
} 
/*主方法调用*/
私有void startDownload(){
Log.v(“计数”、“输出”);
字符串url=lanurl+“泰坦尼克号/视频”+1+“_en.m4v”;
新建DownloadFileAsync().execute(url,“1”);
url=lanurl+“泰坦尼克号/视频”+2+“_en.m4v”;
新建DownloadFileAsync().execute(url,“2”);
}
输出:
在sd卡中下载这两个文件
但是没有正确下载文件

我不理解完整的问题。但你的问题似乎是异步任务


为异步任务创建的单个对象只能使用一次。如果要再次使用它,需要为同一异步任务创建另一个对象。

请为此粘贴整个日志。03-17 13:55:30.554:W/AudioPolicyManager(1315):getSituationVolume()MsitationVolume=0.500000 03-17 13:55:30.554:E/AudioPolicyManager(1315):Speaker_swa Volume=0.000000,device=2,stream=1,索引=0 03-17 13:55:30.562:I/count(7852):in 03-17 13:55:30.570:I/count(7852):in 03-17 13:55:30.601:I/count(7852):in 103-17 13:55:30.617:I/count(7852):in 103-17 13:55:30.726:I/count(7852):in 203-17 13:55:30:30.726:I/count(7852):in203-17 13:55:30.726:7852:I/count(7852):in403-17 13:55:30.757:I/count(7852):in403-17 13:55:33.375:I/WindowOrientationListener(1487):Orientation60.139717给出了新的旋转=1 03-17 13:55:33.375:I/OrientationDebug(1487):[pwm]在MyOrientationListener.onOrientationChanged()旋转=1 mfancRotationAnimation=0现在调用mIndowManager.setRotation()03-17 13:55:33.375:I/OrientationDebug(1487):[wms]在setRotation()中旋转=1 alwaysSendConfiguration=false animFlags=0 03-17 13:55:33.375:I/OrientationDebug(1487):[wms]在setRotation()中现在调用setRotationUnchecked()03-17 13:55:33.375:I/WindowManager(1487):将旋转设置为1,animFlags=0私有void startDownload(){.Log.v(“count”,“out”);字符串url=lanurl+“titanic/video”+1+“_en.m4v”;新下载文件异步().execute(url,“1”);url=lanurl+“titanic/video”+2+“_en.m4v”;新下载文件异步().execute(url,“2”);}请尝试在前一个线程的后期执行中启动新的线程。这样,一次只能维持一个网络连接。是否要继续使用多个连接?请尝试在使用asynctask更新视图中的进度栏时使用ThreadPoolExecutor。我认为ThreadPoolExecutor无法异步更新进度栏视图。在我的日志在4中打印>>