2013-04-19 5 views
0

Im 다운로드를 모두하고 있습니다. 파일 모두를 다운로드하십시오. 내가 한 일은 루프를위한 것이었고, 불행하게도 for 루프는 내 비동기 작업을 끝내지 않는다. 도와주세요. 여기 For 루프가 내 비동기 작업을 완전히 수행하지 않습니다.

for (int i = 0; i < id.length; i++) { 
      Element e = (Element)nodes.item(i); 
      id[i] = ""+CategoriesXMLfunctions.getValue(e, "id"); 
      name[i] = ""+CategoriesXMLfunctions.getValue(e, "name"); 
      image[i] = ""+CategoriesXMLfunctions.getValue(e, "image"); 
      simage[i] = ""+CategoriesXMLfunctions.getValue(e, "simage"); 
      download[i] = ""+CategoriesXMLfunctions.getValue(e, "download"); 
      author[i] = ""+CategoriesXMLfunctions.getValue(e, "author"); 
      genre[i] = ""+CategoriesXMLfunctions.getValue(e, "genre"); 
      size[i] = ""+CategoriesXMLfunctions.getValue(e, "size"); 
      price[i] = ""+CategoriesXMLfunctions.getValue(e, "price"); 
      mylist.add(map);  


     id_all = id[i]; 
     image_all = image[i]; 
     dload_all = download[i]; 
     size_all = size[i]; 
     name_all = name[i]; 
     String response = null; 
     String resstring = null; 
     ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
     postParameters.add(new BasicNameValuePair("devid", "devid")); 
     String devid=null; 

     try { 
       devid = URLEncoder.encode(Global.getMac(), "UTF-8"); 
      } catch (UnsupportedEncodingException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
     try { 
      String nURL = "http://....url"; 
      response = CustomHttpClient.executeHttpPost(nURL, postParameters); 
      resstring=response.toString(); 


      String credit = resstring; 
      String priiice = price[i]; 


      float money = Float.parseFloat(credit); 
      float pri = Float.parseFloat(priiice); 

      if(pri>money){ 
       final AlertDialog.Builder alert = new AlertDialog.Builder(MainStore.this); 
       alert.setMessage("Credits not enough."); 
       alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         dialog.cancel(); 
        } 

       }); 

       alert.show(); 
      }else{ 

       File sd = getDir("xml",MODE_PRIVATE); 

        File todel = new File(sd,id[i]+".xml"); 
        todel.delete(); 
        String filecreat= "/"+id[i]+".xml"; 
        File newfil = new File(sd+ filecreat); 
         if(newfil.exists()){ 
         todel.delete(); 
         } 
        try{ 
        if(!newfil.exists()){ 
        newfil.createNewFile(); 
       }}catch(IOException e1){ 

        } 



       try{ 
        FileWriter fileWritter = new FileWriter(newfil); 
           BufferedWriter bufferWritter = new BufferedWriter(fileWritter); 
           bufferWritter.write("<xml>"); 
           bufferWritter.write("<books>"); 
           bufferWritter.write("<id>"+id[i]+"</id>"); 
           bufferWritter.write("<name>"+name[i]+"</name>"); 
           bufferWritter.write("<download>"+download[i]+"</download>"); 
           bufferWritter.write("<size>"+size[i]+"</size>"); 
           bufferWritter.write("<author>"+author[i]+"</author>"); 
           bufferWritter.write("<genre>"+genre[i]+"</genre>"); 
           bufferWritter.write("<new>0</new>"); 
           bufferWritter.write("</books>"); 
           bufferWritter.write("</xml>"); 
           bufferWritter.close(); 

           Toast.makeText(getApplicationContext(), "Downloading...", Toast.LENGTH_LONG).show();  
         }catch(IOException e1){ 
          e1.printStackTrace(); 
          Toast.makeText(getApplicationContext(), "error wrting xml "+e1.toString(), Toast.LENGTH_LONG).show(); 

         } 

         downloadallStarted(); 
         downloadallfiles(); 
         //checkdownloadall(); 
         //downloadallFinished(); 
      } 


     }catch (Exception e1){ 
      Toast.makeText(getApplicationContext(), "Error in downloadall: " +e1.toString(), Toast.LENGTH_LONG).show(); 
     } 



     } 

그리고 여기 내 비동기 작업입니다 내 루프입니다

직접 (기능 doInBackground에서 변수 dload_all 액세스 할 수 있도록 비동기 작업이 백그라운드 스레드에서 실행
private void startDownloadall() { 
     String fileURL = dload_all; 
     fileURL = fileURL.replace(" ", "%20"); 
     new DownloadFileAsync1().execute(fileURL); 
     Toast.makeText(getApplicationContext(), "Downloading...start all", Toast.LENGTH_LONG).show(); 
    } 
class DownloadFileAsync1 extends AsyncTask<String, String, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      //showDialog(DIALOG_DOWNLOAD_PROGRESS); 
     } 

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

      try { 
       String fileURL = dload_all; 
       fileURL = fileURL.replace(" ", "%20"); 
       URL u = new URL(fileURL); 
       HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
       c.setRequestMethod("GET"); 
       c.setDoOutput(true); 
       c.connect(); 

       String PATH = getDir("ebook", MODE_PRIVATE).toString(); 
       String ebookfile = ""; 
       //String filename = id[index]; 
       String filename = id_all; 
       if(fileURL.endsWith("pdf")) 
        ebookfile = filename+".pdf"; 

       else 
        ebookfile = filename+".epub"; 
       bookfile = ebookfile; 
       Global.setBookfile(bookfile); 
       File file = new File(PATH); 
       file.mkdirs(); 
       File outputFile = new File(file, ebookfile); 
       FileOutputStream fos = new FileOutputStream(outputFile); 

       lenghtOfFilee = c.getContentLength(); 


       InputStream in = c.getInputStream(); 
       byte[] buffer = new byte[1024]; 
       int len1 = 0; 
       long total = 0; 

       while ((len1 = in.read(buffer)) > 0) { 
        total += len1; //total = total + len1 
        //publishProgress("" + (int)((total*100)/lenghtOfFilee)); 
        fos.write(buffer, 0, len1); 
       } 
       fos.close(); 
      } catch (Exception e) { 
       // Log.d("Downloader", e.getMessage()); 
      } 

      return null; 

     } 

     protected void onProgressUpdate(String... progress) { 
      // Log.d("ANDRO_ASYNC",progress[0]); 
      //mProgressDialog.setProgress(Integer.parseInt(progress[0])); 
     } 

     @Override 
     protected void onPostExecute(String unused) { 
      Global.setBookfile(bookfile); 
      Toast.makeText(getApplicationContext(), "Downloading..."+lenghtOfFilee, Toast.LENGTH_LONG).show(); 
      checkdownloadall(); 
      //dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
     } 

    } 
+0

이것은 당신을 도울 수있다 [http://stackoverflow.com/questions/15052153/why-only-1-of-10-image-is-load-and-saved-in- sd - 카드] –

답변

0

) 그 스레드로부터 안전하지 않습니다. 어쩌면 당신은 시도 할 수있다 :

private void startDownloadall() { 
    //String fileURL = dload_all; 
    //fileURL = fileURL.replace(" ", "%20"); 
    new DownloadFileAsync1().execute(dload_all, id_all); 
    Toast.makeText(getApplicationContext(), "Downloading...start all", Toast.LENGTH_LONG).show(); 
} 
class DownloadFileAsync1 extends AsyncTask<String, String, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     //showDialog(DIALOG_DOWNLOAD_PROGRESS); 
    } 

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

     try { 
      String fileURL = args[0]; 
      fileURL = fileURL.replace(" ", "%20"); 
      URL u = new URL(fileURL); 
      HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
      c.setRequestMethod("GET"); 
      c.setDoOutput(true); 
      c.connect(); 

      String PATH = getDir("ebook", MODE_PRIVATE).toString(); 
      String ebookfile = ""; 
      //String filename = id[index]; 
      String filename = args[1]; 
      if(fileURL.endsWith("pdf")) 
       ebookfile = filename+".pdf"; 

      else 
       ebookfile = filename+".epub"; 
      bookfile = ebookfile; 
      Global.setBookfile(bookfile); 
      File file = new File(PATH); 
      file.mkdirs(); 
      File outputFile = new File(file, ebookfile); 
      FileOutputStream fos = new FileOutputStream(outputFile); 

      lenghtOfFilee = c.getContentLength(); 


      InputStream in = c.getInputStream(); 
      byte[] buffer = new byte[1024]; 
      int len1 = 0; 
      long total = 0; 

      while ((len1 = in.read(buffer)) > 0) { 
       total += len1; //total = total + len1 
       //publishProgress("" + (int)((total*100)/lenghtOfFilee)); 
       fos.write(buffer, 0, len1); 
      } 
      fos.close(); 
     } catch (Exception e) { 
      // Log.d("Downloader", e.getMessage()); 
     } 

     return null; 

    } 

    protected void onProgressUpdate(String... progress) { 
     // Log.d("ANDRO_ASYNC",progress[0]); 
     //mProgressDialog.setProgress(Integer.parseInt(progress[0])); 
    } 

    @Override 
    protected void onPostExecute(String unused) { 
     Global.setBookfile(bookfile); 
     Toast.makeText(getApplicationContext(), "Downloading..."+lenghtOfFilee, Toast.LENGTH_LONG).show(); 
     checkdownloadall(); 
     //dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
    } 

} 
+0

그것은 작동하지 않았다 : (참고, 내 비동기 작업은 'downloadallfiles()'함수 내부라고합니다 – belladonna

관련 문제