-1

我正在从 FTP 服务器下载文件,但我得到一个NullPointerException. 我究竟做错了什么?

public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  

    ftpConnect("domain","username", "password", portnum);  

    try  
    {  
         String s = Environment.getExternalStorageDirectory().toString();  
         File f = new File(s+"/???/item");  
         f.mkdirs();  
         ftpDownload("ftp://user@portalftp.?????.com/rooms.txt", f);  
    }  
    catch(Exception e)  
    {  
         Toast toast = Toast.makeText(getApplicationContext(), "Download error: "+e.getMessage(), Toast.LENGTH_SHORT);  
         toast.show();  
    }  

    ftpDisconnect();  
}  

public boolean ftpDownload(String srcFilePath, File desFilePath)  
{  
    boolean status = false;  
    try {  
        FileOutputStream desFileStream = new FileOutputStream(desFilePath);  

        status = mFTPClient.retrieveFile(srcFilePath, desFileStream);  
        desFileStream.close();  
        Toast toast = Toast.makeText(getApplicationContext(), "Downloaded", Toast.LENGTH_SHORT);  
        toast.show();  

        return status;  
        } catch (Exception e) {  
        Toast toast = Toast.makeText(getApplicationContext(), "Download error"+e.getMessage(), Toast.LENGTH_SHORT);  
        toast.show();  
    }  

    return status;  
}  

 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);  

            /* Set File Transfer Mode 
             * 
             * To avoid corruption issue you must specified a correct 
             * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE, 
             * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE 
             * for transferring text, image, and compressed files. 
             */  

            mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);  
            mFTPClient.enterLocalPassiveMode();  

            return status;  
        }  
    } catch(Exception e) {  
        Log.e("ERROR", e.getMessage());  
    }  
    return false;  
 }  
}  
4

1 回答 1

2

尝试使用这个:

Log.e("ERROR", String.valueOf(e.getMessage()));

这样,如果消息为空,您将得到“ERROR”、“null”的输出。

此外,你最好听听那些建议你在主线程上做你的网络相关工作的人。

于 2013-04-23T13:51:26.970 回答