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

そうすれば、メッセージが null の場合、「ERROR」、「null」の出力が得られます。

また、メイン スレッドでネットワーク関連の作業を行うことを提案する人々の意見に耳を傾けるのも賢明です。

于 2013-04-23T13:51:26.970 に答える