2

我正在尝试从 PHP 服务器下载一堆文件并将它们存储在 sdcard 上。但是当我下载文件时,我收到以下错误: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 android.os.AsyncTask$3@434ef2c0 rejected from java.util.concurrent.ThreadPoolExecutor@4364f0d0[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 android.os.AsyncTask$3@43605840 rejected from java.util.concurrent.ThreadPoolExecutor@4364f0d0[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;
            }
        }
4

1 回答 1

1

一堆是你的问题。您超出了执行程序池限制(盲目猜测是您同时运行了 10 多个)。使用IntentServer按顺序下载,或者DownloadManager如果您的目标 API 已经支持它,请使用。

于 2013-06-26T17:49:27.880 回答