将connectiong android app连接到ftp时发生FatalException

时间:2013-11-13 15:09:04

标签: android ftp

我是Android编程新手。我希望我的应用程序连接到FTP并显示工作目录 所以我复制了这段代码并实现了它,但我得到了一个致命的例外。我不知道它是什么或如何解决它.LogCat如下。

 public class MainActivity extends Activity
   {


    Button but; 
    @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        but=(Button)findViewById(R.id.button1);
        .setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            try
        {
            down d = new down();
            d.execute();

        }
            catch (Exception e)
        {
            int d = Toast.LENGTH_SHORT;
            String c =e.toString();
            Toast toast = Toast.makeText(getApplicationContext(),c,d);
            toast.show();
        }


        }
    });
}
public class down extends AsyncTask<String, Void, String>
{
public FTPClient mFTPClient = null;

    private String username = "******";
    private String password = "**********";
    private String ftpServer = "31.******";
    private Integer ftpPort = 21; 
 @Override
 protected String doInBackground(String... empty) {
 String strResponce = "";
 Boolean connectStatus = ftpConnect(ftpServer, username,password, ftpPort);
 if (connectStatus == true){
 String src = ftpGetCurrentWorkingDirectory().toString()+"/ftpserverfile.txt"; 
 String dest = "ftpserverfile.txt";

 ftpPrintFilesList(ftpGetCurrentWorkingDirectory());
 System.out.println("Downloading "+src+" to "+dest);
 downloada d = new downloada();
 Boolean ftpDownloadStatus = d.ftpDownload(src, dest);

 if (ftpDownloadStatus == true){
 strResponce = "true";
 }
 else{
 strResponce = "false";
}
 }
 ftpDisconnect();

 return strResponce;
 }
 private void ftpPrintFilesList(String ftpGetCurrentWorkingDirectory) {
// TODO Auto-generated method stub
 System.out.print(ftpGetCurrentWorkingDirectory);

}
 @Override
 protected void onPostExecute(String result) {

 }
 public boolean ftpConnect(String host, String username,
     String password, int port)
        {
         try {
         mFTPClient = new FTPClient();
         // connecting to the host
         mFTPClient.connect(host, port);

         // now check the reply code, if positive mean connection success
         if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
         // login using username & password
         boolean status = mFTPClient.login(username, password);
         mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
         mFTPClient.enterLocalPassiveMode();
         return status;
         }

         } 
                     catch(Exception e) {
         System.out.println("Exception connecting to FTP server:" + e);
         }

        return false;
        }
        /* Method to disconnect from FTP server */
        public boolean ftpDisconnect()
        {
         try {
         mFTPClient.logout();
         mFTPClient.disconnect();
         return true;
         } 
         catch (Exception e) {
         System.out.println("Exception disconnecting from FTP server:" + e);
         }

         return false;
        }
        /* Method to get current working directory */
        public String ftpGetCurrentWorkingDirectory()
        {
         try {
     String workingDir = mFTPClient.printWorkingDirectory();
         return workingDir;
         } catch(Exception e) {
         System.out.println("Exception getting current working directory:" + e);
         }

         return null;
        }


 public class downloada extends Activity
 {
    public boolean ftpDownload(String srcFilePath, String desFilePath)
        {
         boolean status = false;
         try {
        // FileOutputStream desFileStream = new FileOutputStream(desFilePath);
         FileOutputStream desFileStream = openFileOutput(desFilePath,                 Context.MODE_PRIVATE);

         status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
             desFileStream.close();

         return status;
         } catch (Exception e) {
         System.out.println("download failed: "+e);
         }

         return status;
        }


}
}

例外是:

 I/System.out(977): /Downloading //ftpserverfile.txt to ftpserverfile.txt
 W/dalvikvm(977): threadid=9: thread exiting with uncaught exception (     group=0x40014760)
E/AndroidRuntime(977): FATAL EXCEPTION: AsyncTask #1
AndroidRuntime(977): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(977):  at android.os.AsyncTask$3.done(AsyncTask.java:266)
 E/AndroidRuntime(977):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
E/AndroidRuntime(977):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
E/AndroidRuntime(977):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
E/AndroidRuntime(977):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
E/AndroidRuntime(977):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
E/AndroidRuntime(977):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
E/AndroidRuntime(977):  at java.lang.Thread.run(Thread.java:1020)
E/AndroidRuntime(977): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
E/AndroidRuntime(977):  at android.os.Handler.<init>(Handler.java:121)
E/AndroidRuntime(977):  at android.app.Activity.<init>(Activity.java:728)
E/AndroidRuntime(977):  at com.example.ftpconnection.MainActivity$down$downloada.<init>(MainActivity.java:149)
E/AndroidRuntime(977):  at  com.example.ftpconnection.MainActivity$down.doInBackground(MainActivity.java:68)
E/AndroidRuntime(977):  at    com.example.ftpconnection.MainActivity$down.doInBackground(MainActivity.java:1)  
E/AndroidRuntime(977):  at android.os.AsyncTask$2.call(AsyncTask.java:252)
E/AndroidRuntime(977):  at       java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
E/AndroidRuntime(977):  ... 4 more

1 个答案:

答案 0 :(得分:1)

试试这个

public class downloada{...}

这里不需要“扩展活动”。你甚至不需要课程“downloada”。您只能使用此功能

public boolean ftpDownload(String srcFilePath, String desFilePath){...}