我正在从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;
}
}
答案 0 :(得分:2)
尝试使用:
Log.e("ERROR", String.valueOf(e.getMessage()));
这样,如果消息为null,您将得到“ERROR”,“null”的输出。
另外,你最好倾听那些建议你在主线程上做网络相关事情的人。