2

我想從PHP服務器下載一堆文件並將它們存儲在SD卡上。但是當我下載文件時,出現以下錯誤:No of files download from server is **500 -600** after download some files **250 - 260**。它提出了一個錯誤。從服務器下載一堆文件

錯誤

07-10 08:40:46.228: E/File Error(1837): File id is 287 File path is http://192.168.1.21/SOCH/upload/chapter_36/5.9.pdf Synch Path is /mnt/sdcard/SOCH/36/ 
    07-10 08:40:46.268: W/System.err(1837): java.util.concurrent.RejectedExecutionException: Task [email protected] rejected from [email protected][Running, pool size = 128, active threads = 128, queued tasks = 128, completed tasks = 1] 
    07-10 08:40:46.268: W/System.err(1837):  at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1967) 
    07-10 08:40:46.268: W/System.err(1837):  at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:782) 
    07-10 08:40:46.268: W/System.err(1837):  at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1303) 
    07-10 08:40:46.268: W/System.err(1837):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:589) 
    07-10 08:40:46.268: W/System.err(1837):  at com.soch.webservice.FileTransferService.onStartCommand(FileTransferService.java:146) 
    07-10 08:40:46.278: W/System.err(1837):  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490) 
    07-10 08:40:46.278: W/System.err(1837):  at android.app.ActivityThread.access$1900(ActivityThread.java:130) 
    07-10 08:40:46.278: W/System.err(1837):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) 
    07-10 08:40:46.278: W/System.err(1837):  at android.os.Handler.dispatchMessage(Handler.java:99) 
    07-10 08:40:46.278: W/System.err(1837):  at android.os.Looper.loop(Looper.java:137) 
    07-10 08:40:46.278: W/System.err(1837):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
    07-10 08:40:46.278: W/System.err(1837):  at java.lang.reflect.Method.invokeNative(Native Method) 
    07-10 08:40:46.278: W/System.err(1837):  at java.lang.reflect.Method.invoke(Method.java:511) 
    07-10 08:40:46.278: W/System.err(1837):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
    07-10 08:40:46.278: W/System.err(1837):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
    07-10 08:40:46.278: W/System.err(1837):  at dalvik.system.NativeStart.main(Native Method) 
    07-10 08:40:46.278: E/File Error(1837): File id is 288 File path is http://192.168.1.21/SOCH/upload/chapter_37/6.1.pdf Synch Path is /mnt/sdcard/SOCH/37/ 
    07-10 08:40:46.308: W/System.err(1837): java.util.concurrent.RejectedExecutionException: Task [email protected] rejected from [email protected][Running, pool size = 128, active threads = 128, queued tasks = 128, completed tasks = 1] 
    07-10 08:40:46.308: W/System.err(1837):  at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1967) 
    07-10 08:40:46.308: W/System.err(1837):  at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:782) 
    07-10 08:40:46.308: W/System.err(1837):  at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1303) 
    07-10 08:40:46.308: W/System.err(1837):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:589) 
    07-10 08:40:46.308: W/System.err(1837):  at com.soch.webservice.FileTransferService.onStartCommand(FileTransferService.java:146) 
    07-10 08:40:46.308: W/System.err(1837):  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490) 
    07-10 08:40:46.308: W/System.err(1837):  at android.app.ActivityThread.access$1900(ActivityThread.java:130) 
    07-10 08:40:46.308: W/System.err(1837):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) 
    07-10 08:40:46.308: W/System.err(1837):  at android.os.Handler.dispatchMessage(Handler.java:99) 
    07-10 08:40:46.308: W/System.err(1837):  at android.os.Looper.loop(Looper.java:137) 
    07-10 08:40:46.308: W/System.err(1837):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
    07-10 08:40:46.308: W/System.err(1837):  at java.lang.reflect.Method.invokeNative(Native Method) 
    07-10 08:40:46.308: W/System.err(1837):  at java.lang.reflect.Method.invoke(Method.java:511) 
    07-10 08:40:46.308: W/System.err(1837):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
    07-10 08:40:46.308: W/System.err(1837):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
    07-10 08:40:46.308: W/System.err(1837):  at dalvik.system.NativeStart.main(Native Method) 

任何人可以幫助我解決這個問題?我正在使用asynchtask進行下載。

我的代碼是

包com.webservice;

public class FileTransferService extends Service { 

     private static final int CORE_POOL_SIZE = 1; 
    private static final int MAXIMUM_POOL_SIZE = 128; 
    private static final int KEEP_ALIVE = 128 ; 

    private static final BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<Runnable>(MAXIMUM_POOL_SIZE); 

    private static final ThreadFactory sThreadFactory = new ThreadFactory() { 
      private final AtomicInteger mCount = new AtomicInteger(1); 

      public Thread newThread(Runnable r) { 
       return new Thread(r, "AsyncTask #" + mCount.getAndIncrement()); 
      } 
     }; 


    public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,TimeUnit.SECONDS, sPoolWorkQueue,sThreadFactory); 

      int FileNo = 0; 

      ArrayList<String> FileId = new ArrayList<String>(); 
      ArrayList<String> ServerPath = new ArrayList<String>(); 
      ArrayList<String> SynchPath = new ArrayList<String>(); 
      ArrayList<String> FileNameList = new ArrayList<String>(); 

      String FileTransferPath = "", SDcardPath = ""; 

      @Override 
      public void onCreate() { 
       super.onCreate(); 
       Log.d("Service Created", "Successfully"); 

       FileId.clear(); 
       ServerPath.clear(); 
       SynchPath.clear(); 
       FileNameList.clear(); 

       SDcardPath = Environment.getExternalStorageDirectory().getPath() + "/"; 

       // Get FileTransfer Link From The SD Card 
       ...... 

       // Getting A url From DB 

       ..... 

      }// End onCreate 

      @Override 
      public int onStartCommand(Intent intent, int flags, int startId) { 

       Log.d("Service Started", "Successfully"); 

       Log.d("fileSize",""+FileId.size()); 
         if (FileId.size() > 0) { 

        ContentValues mContentValuesUpdatefileStatus = new ContentValues(); 
        mContentValuesUpdatefileStatus.put("Status", 1); 

         while (FileNo < FileId.size()) { 

         dbh.updateRecord("Mst_FileTransfer",mContentValuesUpdatefileStatus,"FileTransferId",FileId.get(FileNo)); 

         try { 

          new DownloadFileFromURL().executeOnExecutor(THREAD_POOL_EXECUTOR,Integer.toString(FileNo));       
          //new DownloadFileFromURL().execute(Integer.toString(FileNo));     

         } catch (Exception e) {  
          e.printStackTrace(); 
          Log.e("File Error", " File id is " + FileId.get(FileNo) + " File path is " + ServerPath.get(FileNo) + " Synch Path is " + SynchPath.get(FileNo)); 
         } 

         FileNo +=1; 
        } 

       } 

       Log.d("Service Finish", "Successfully"); 
       stopSelf(); 

       return super.onStartCommand(intent, flags, startId); 
      } 

      class DownloadFileFromURL extends AsyncTask<String, String, String> { 

       int Count = 0; 
       int File_Completed = 0; 
       int FileNo; 
       String strServerPath,strSynchPath,strFileName; 

       @Override 
       protected void onPreExecute() { 
        super.onPreExecute(); 

       } 

       @Override 
       protected synchronized String doInBackground(String... params) { 

        FileNo = Integer.parseInt(params[0]); 

        // Get File Path 
        strServerPath = ServerPath.get(FileNo).replaceAll(" ", "%20"); 
        strSynchPath = SynchPath.get(FileNo); 
        strFileName = FileNameList.get(FileNo); 

        URLConnection mConnection = null; 

        Log.d("file", strSynchPath + strFileName); 

        File SynchPath = new File(strSynchPath); 
        if (!SynchPath.exists()) { 
         SynchPath.mkdirs(); 
        } 

        try { 

         URL url = new URL(strServerPath); 
         mConnection = url.openConnection(); 
         mConnection.connect(); 

         // getting file length 
         int lenghtOfFile = mConnection.getContentLength(); 

         // Output stream to write file 
         File outputFile = new File(SynchPath, strFileName); 
         FileOutputStream mFileOutputStream = new FileOutputStream(
           outputFile); 

         InputStream mInputStream = url.openStream(); 
         byte data[] = new byte[1024]; 

         long File_Completed_Size = 0; 

         while ((Count = mInputStream.read(data)) != -1) { 

          File_Completed_Size += Count; 
          File_Completed = (int) ((File_Completed_Size * 100)/lenghtOfFile); 

          // writing data to file 
          mFileOutputStream.write(data, 0, Count); 
         } 

         // flushing output 
         mFileOutputStream.flush(); 

         // closing streams 
         mFileOutputStream.close(); 
         mInputStream.close(); 

        } catch (IOException IO) { 

         Log.e("File Exception"," File id is " + FileId.get(FileNo) + " File path is " + strServerPath + " Synch Path is " + strSynchPath + "FileName " + strFileName); 
         IO.printStackTrace(); 

        } catch (Exception e) { 

         Log.e("File Exception"," File id is " + FileId.get(FileNo) + " File path is " + strServerPath + " Synch Path is " + strSynchPath + "FileName " + strFileName); 
         e.printStackTrace(); 
        } 
        return null; 
       } 

       @Override 
       protected void onPostExecute(String result) { 
        super.onPostExecute(result); 

        if (File_Completed == 100) { 
         Log.d("File Downlode Status for " + SynchPath.get(FileNo), "" + File_Completed + "%"); 
        } 
       } 
      } 

      @Override 
      public IBinder onBind(Intent intent) { 
       return null; 
      } 
     } 
+0

你能分享一些代碼嗎? – Bun

回答

1

一堆是你的問題。您超出了執行者池限制(盲目猜測您同時運行了10多個)。使用IntentServer按順序進行下載,如果您已經支持已經支持的API,則使用DownloadManager

+0

我正在4.1上工作但我不知道如何做線程池限制..你可以舉一些例子plz ... –

+0

你真的不能提升操作系統限制。你是否真的**需要並行獲取數據?我非常懷疑 - 移動設備的速度不會更快。擺脫'AsyncTask'並使用'IntentService'做下載 –

+0

在我的情況下,我不能使用DownloadManager下載文件,因爲我不想給我的用戶任何系統訪問權限...基本上我正在開發1學校的應用程序,並在我的應用程序,我沒有授予用戶訪問系統的權利... –