2014-06-09 51 views
-1

我有一個asyntask發送請求並接收圖像。它工作正常的1項,事情是在這裏,我有這種方法:使用for循環的asynctask

Arraylist<String> todownload; 
    . 
    . 

    public void downloadit(){ 


    int size=todownload.size(); 
    for (int i=0;i<size;i++) { 
      this.todo=todownload.get(i); 
      System.out.println("Dowloading = " + todo); 
    MyAsyncImagesDownload sync = new MyAsyncImagesDownload(); 
System.out.println("Size="+todownload.size()); 
    sync.execute(); 

    } 

使其工作下載多個請求。但讓說todownload有2個值內,待辦事項的值,它是一個誰異步工作着,在服務器總是最後一個,我也試圖與更換for循環:

for(String todo:Todownload) 
. 
. 

仍一樣。所以我覺得循環在完成執行任務之前完成了它的工作,所以我在執行之後添加了Wait方法,並在異步任務結束時添加了Notifyall(),但似乎我得到了一些錯誤。你能幫我一下,我能做些什麼來達到這個目的?感謝

異步代碼:

BufferedOutputStream bos; 
    OutputStream output; 
    DataOutputStream dos; 
    int len; 
    int smblen; 
    InputStream in; 
    DataInputStream clientData; 
    FileOutputStream fos; 
    String filepath; 
    String target; 
    Socket clientSocket; 




    @Override 
    protected String doInBackground(String... Result) { 
     // TODO Auto-generated method stub 



String REresponse = null; 


Log.i("lifemate","im here in the Async !"); 
try { 
    socket = new Socket(server, 1450); 
} catch (UnknownHostException e5) { 
    // TODO Auto-generated catch block 
    e5.printStackTrace(); 
} catch (IOException e5) { 
    // TODO Auto-generated catch block 
    e5.printStackTrace(); 
} 

String msg = "Connection accepted " + socket.getInetAddress() + ":" +  
socket.getPort(); 
    Log.i(LOGTAG, msg); 

    try { 
    sInput = new ObjectInputStream(socket.getInputStream()); 
} catch (StreamCorruptedException e4) { 
    // TODO Auto-generated catch block 
    e4.printStackTrace(); 
} catch (IOException e4) { 
    // TODO Auto-generated catch block 
    e4.printStackTrace(); 
} 
try { 
    sOutput = new ObjectOutputStream(socket.getOutputStream()); 
} catch (IOException e3) { 
    // TODO Auto-generated catch block 
    e3.printStackTrace(); 
} 

try { 


sOutput.writeObject(todo); 

received=false; 

} catch (IOException e2) { 
// TODO Auto-generated catch block 
e2.printStackTrace(); 
} 

try { 
sOutput.flush(); 
} catch (IOException e1) { 
// TODO Auto-generated catch block 
e1.printStackTrace(); 
} 
    //Log.i(LOGTAG,"this"+rdytosplit); 

while(received==false) 
{ 



clientSocket = socket; 

try { 
in = clientSocket.getInputStream(); 
} catch (IOException e2) { 
// TODO Auto-generated catch block 
e2.printStackTrace(); 
} 

clientData = new DataInputStream(in); //used  
// clientBuff = new BufferedInputStream(in); //use  
System.out.println("i have started"); 

int N=1; 
while(N==1){  

    System.out.println("Starting...");  




      int fileSize = 0; 
      try { 
       fileSize = clientData.read(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      }   
      fileSize = (fileSize > 0) ? fileSize :0; 
                     // guard 
against 
negatives. 
      List<File> files = new ArrayList<>(fileSize);   //store list of 
filename from client directory  // Using List and <> 
      List<Integer> sizes = new ArrayList<>(fileSize);  //store file size 
from client 
     //Start to accept those filename from server 

      for (int count=0;count < fileSize;count ++){ 
       File ff = null; 
       try { 
        ff = new File(clientData.readUTF()); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       files.add(ff); 
       filepath="storage/sdcard/Pictures/"; 




      } 

    for (int count=0;count < fileSize;count ++){ 

      try { 
       sizes.add(clientData.readInt()); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
    } 

    for (int count =0;count < fileSize ;count ++){  

    len=sizes.get(count); 

    System.out.println("File Size ="+len); 



    try { 
     output = new FileOutputStream(filepath + files.get(count)); 
    } catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    dos=new DataOutputStream(output); 
    bos=new BufferedOutputStream(output); 

    byte[] buffer = new byte[1024];  

    try { 
     bos.write(buffer, 0, buffer.length); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } //This line is important 

    try { 
     while (len > 0 && (smblen = clientData.read(buffer)) > 0) { 
      try { 
       dos.write(buffer, 0, smblen); 
      ////here you must add +1 value to imagechange_1 or ..._2 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
       len = len - smblen; 
       try { 
       dos.flush(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }  
    Log.i("downloader","job is done"); 
    N=2; 

    try { 
    dos.close(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
    //// should i close in and clientdata now ? 
    } 



} //end loop 
received=true; 
} 






try { 
in.close(); 
} catch (IOException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
try { 
socket.close(); 
} catch (IOException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
Run=false; 
disconnect(); 

return REresponse; 

    } 

}

+0

什麼是'MyAsincImagesDownload' ?哪個類包含'downloadit()'方法? –

+0

其主要包含downloadit()和第一個是myAsync名稱的課程:) – user3694470

+0

請發佈'MyAsyncImagesDownload'的代碼。 –

回答

0

你爲什麼不這樣做的loopingasynctask內,而不是循環的asynctask的:

private class MyAsyncImagesDownload extends AsyncTask<URL, Integer, Long> { 
    protected Long doInBackground(URL... urls) { 
     int count = urls.length; 
     long totalSize = 0; 

     //a loop can be like this 
     for (int i = 0; i < count; i++) { 
      totalSize += Downloader.downloadFile(urls[i]); 
      publishProgress((int) ((i/(float) count) * 100)); 
      if (isCancelled()) break; 
     } 
     return totalSize; 
    } 


    protected void onPostExecute(Long result) { 
     showDialog("Downloaded " + result + " bytes"); 
    } 
} 
+0

我試過了,結果令人驚訝的失敗,我認爲它發出了3個數組的請求,其中1個爲空 – user3694470

+0

哦,我沒有嘗試像這樣的課程,讓我們試試吧 – user3694470

+0

你的循環在'asynctask'中的樣子是什麼 –