-1
我正在從FTP服務器上下載文件,但是我得到了一個NullPointerException
。我究竟做錯了什麼?java.lang.NullPointerException:println需要一條FTP下載的消息
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://[email protected]?????.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;
}
}
我們需要一個堆棧跟蹤...告訴我們哪一行出錯了。 – 2013-04-23 13:39:48
'我在做什麼錯誤?'...第一:你在主線程上做網絡相關的東西... – Selvin 2013-04-23 13:43:02
如果傳遞的消息爲空,Log.e會拋出異常。確保'e.getMessage()'不爲空 – 2013-04-23 13:43:50