没有数据传输应用停止工作

时间:2014-02-17 17:38:46

标签: android multithreading android-asynctask

我有一个活动(MyFTPClient)连接到FTP服务器并异步下载文件列表。 MyFTPClient.java

@Override
       protected void onPreExecute(){
           dialog = ProgressDialog.show(MyFTPClient.this,null,"Please wait...");

        }

       protected ArrayList<String> doInBackground(String... connection) {


            XmlSerializer serializer = Xml.newSerializer();
            StringWriter writer = new StringWriter();

            Activity context = MyFTPClient.this;
            final ConnectivityManager connec = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);


                if(connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
                        connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED)
                {
                    File sdCard = Environment.getExternalStorageDirectory();
                    File directory = new File(sdCard.getAbsolutePath()+"/FTPClient/FTPCL.xml"); 

                    temparrlist=listftpitems("/public_html/");

                    if(temparrlist!= null){

                        sizelist = listftpfilesize("/public_html/");

                        if(sizelist!=null){
                            try {
                            //xml file creation
                        }
                    }
                    else{
                        //dialog.dismiss();
                        MyFTPClient.this.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                AlertDialog.Builder builder = new AlertDialog.Builder(MyFTPClient.this);
                                    builder.setTitle("Connection problem")
                                    .setMessage("Problem in connecting to server")
                                    .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.cancel();  
                                        }
                                    });
                                    AlertDialog alert = builder.create();
                                    alert.show();
                        }});


                    }

             }
                else if(connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
                        connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
                        connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
                        connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  )
                    {
                        dialog.dismiss();
                        MyFTPClient.this.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                AlertDialog.Builder builder = new AlertDialog.Builder(MyFTPClient.this);
                                    builder.setTitle("Connection problem")
                                    .setMessage("Unable connect to host server. Please Check your Internet Connectivity.")
                                    .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.cancel();  
                                        }
                                    });
                                    AlertDialog alert = builder.create();
                                    alert.show();
                        }});
                       return null;
            }       
            return temparrlist;    
        }

    @Override
    protected void onPostExecute(ArrayList<String>result) {
           dialog.dismiss();
       }

listfile方法是

public ArrayList<String> listftpitems(String dir_path){
     ArrayList<String> arrayList = new ArrayList<String>();

    try {
        mFTPClient = new FTPClient();
        mFTPClient.connect("ftp.abcd.com",21);
        boolean status = mFTPClient.login("usrname", "pass");
        mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
        mFTPClient.enterLocalPassiveMode();

                if (status == true) {
                    Log.d(TAG, "Connection Success");
                    final FTPFile[] files;
                    try {
                        files = mFTPClient.listFiles("/public_html/");
                        for (FTPFile file : files) {
                            String details = file.getName();

                            boolean isFile = file.isFile();

                            if (isFile) {
                                Log.d(TAG, details);
                                arrayList.add(details);
                                }
                           }
                    }catch (IOException e1) {
                       e1.printStackTrace();
                    }   

            }
                else {
                    Log.d(TAG, "Connection failed");
                }
            } catch (Exception e) {
                e.printStackTrace();
                Log.d(TAG, "Unable to connect");
                dialog.dismiss();
        }
    return arrayList;

    }

因此,当移动设备连接到互联网但没有数据传输时,应用程序就会崩溃。

logcat是

02-17 22:48:43.276: W/System.err(17703): java.net.UnknownHostException: Host is unresolved: ftp.andi.site11.com
02-17 22:48:43.276: W/System.err(17703):    at java.net.Socket.connect(Socket.java:855)
02-17 22:48:43.286: W/System.err(17703):    at org.apache.commons.net.SocketClient.connect(SocketClient.java:176)
02-17 22:48:43.306: W/System.err(17703):    at com.example.ftpclient.MyFTPClient.listftpitems(MyFTPClient.java:258)
02-17 22:48:43.316: W/System.err(17703):    at com.example.ftpclient.MyFTPClient$connection_test.doInBackground(MyFTPClient.java:118)
02-17 22:48:43.346: W/System.err(17703):    at com.example.ftpclient.MyFTPClient$connection_test.doInBackground(MyFTPClient.java:1)
02-17 22:48:43.346: W/System.err(17703):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
02-17 22:48:43.356: W/System.err(17703):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-17 22:48:43.356: W/System.err(17703):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-17 22:48:43.386: W/System.err(17703):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
02-17 22:48:43.406: W/System.err(17703):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-17 22:48:43.406: W/System.err(17703):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-17 22:48:43.416: W/System.err(17703):    at java.lang.Thread.run(Thread.java:864)
02-17 22:48:43.416: D/MyFTPClient(17703): Unable to connect
02-17 22:48:43.426: D/memalloc(17703): /dev/pmem: Unmapping buffer base:0x54333000 size:6574080 offset:6266880
02-17 22:48:43.426: D/memalloc(17703): /dev/pmem: Unmapping buffer base:0x56187000 size:6881280 offset:6574080
02-17 22:48:43.426: D/memalloc(17703): /dev/pmem: Unmapping buffer base:0x58114000 size:7188480 offset:6881280
02-17 22:48:43.576: W/dalvikvm(17703): threadid=11: thread exiting with uncaught exception (group=0x40a7d228)
02-17 22:48:43.626: E/AndroidRuntime(17703): FATAL EXCEPTION: AsyncTask #1
02-17 22:48:43.626: E/AndroidRuntime(17703): java.lang.RuntimeException: An error occured while executing doInBackground()
02-17 22:48:43.626: E/AndroidRuntime(17703):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at java.lang.Thread.run(Thread.java:864)
02-17 22:48:43.626: E/AndroidRuntime(17703): Caused by: java.lang.NullPointerException
02-17 22:48:43.626: E/AndroidRuntime(17703):    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:471)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:534)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:583)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at org.apache.commons.net.ftp.FTP.syst(FTP.java:1393)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at org.apache.commons.net.ftp.FTPClient.getSystemName(FTPClient.java:1849)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:2263)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2046)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at com.example.ftpclient.MyFTPClient.listftpfilesize(MyFTPClient.java:326)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at com.example.ftpclient.MyFTPClient.access$0(MyFTPClient.java:322)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at com.example.ftpclient.MyFTPClient$connection_test.doInBackground(MyFTPClient.java:122)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at com.example.ftpclient.MyFTPClient$connection_test.doInBackground(MyFTPClient.java:1)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
02-17 22:48:43.626: E/AndroidRuntime(17703):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-17 22:48:43.626: E/AndroidRuntime(17703):    ... 5 more

listftpitem和listftpfilesize

public ArrayList<String> listftpitems(String dir_path){
         ArrayList<String> arrayList = new ArrayList<String>();

        try {
            mFTPClient = new FTPClient();
            mFTPClient.setConnectTimeout(5000);
            //mFTPClient.setDefaultTimeout(10*1000);
            mFTPClient.connect("ftp.andi.site11.com",21);
            boolean status = mFTPClient.login("a7078185", "hack@44");
            mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
            mFTPClient.enterLocalPassiveMode();

                    if (status == true) {
                        Log.d(TAG, "Connection Success");
                        final FTPFile[] files;
                        try {
                            files = mFTPClient.listFiles("/public_html/");
                            for (FTPFile file : files) {
                                String details = file.getName();

                                boolean isFile = file.isFile();

                                if (isFile) {
                                    Log.d(TAG, details);
                                    arrayList.add(details);
                                    }
                               }
                        }catch (IOException e1) {
                           e1.printStackTrace();
                        }   

                }
                    else {
                        Log.d(TAG, "Connection failed");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d(TAG, "Unable to connect");

            }
        return arrayList;

        }

     private ArrayList<Integer> listftpfilesize(String string) {
         ArrayList<Integer> sizeList = new ArrayList<Integer>();
         final FTPFile[] filesz;
            try {
                filesz = mFTPClient.listFiles("/public_html/");
                for (FTPFile file : filesz) {
                    long details = file.getSize();
                    long size = details/1024;
                    boolean isFile = file.isFile();

                    if (isFile) {
                        Log.d(TAG, String.format("EAN-8: %d",size ));
                        sizeList.add((int) size);
                        }
                   }
            }catch (IOException e1) {
                e1.printStackTrace();   
            }   
         return sizeList;
        }

我无法修复错误。谢谢。

0 个答案:

没有答案