java ftp ftpclient,Java的应用程序ftpclient不连接

在尝试使用Java和Android连接FTP服务器时遇到问题,代码无法执行到FTPClient.connect方法并跳转到异常处理部分,显示null异常和0回复码。问题在于从主线程执行了网络操作,这在Android 3.0及以上版本中是不允许的。解决方案是添加INTERNET权限到AndroidManifest.xml文件,并使用AsyncTask在后台线程执行FTP连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am new to android and java programing. I write following code to connect to ftp server

public class NewMainActivity extends Activity {

TextView Display;

//declare a public FTP client object.

public FTPClient mFTPClient = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_new_main);

final TextView Display = (TextView) findViewById(R.id.tvResults);

try {

mFTPClient = new FTPClient();

// connecting to the host

mFTPClient.connect("www.filegenie.com",21);

// now check the reply code, if positive mean connection success

if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {

// login using username & password

boolean status = mFTPClient.login("user", "MyPassword");

// Set File Transfer Mode

mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);

mFTPClient.enterLocalPassiveMode();

Display.setText("Correct"+ String.valueOf(status)); //return status;

}

}catch(Exception e) {

Display.setText(" Exception message is = "+e.getMessage()+" Reply code = "+mFTPClient.getReplyCode());

}

}

When I run this code I get no specific error but the program didn't execute mFTPClient.connect method and jumps to the 'Exception' part and I get "null" as exception getMessage and "0" as ReplyCode.

I have import all the required files such as

import org.apache.commons.net.ftp.FTP;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPReply;

I am using eclipse and ADT Build v22.0.1-685705.

I have also included commons-net-3.3, commons-net-3.3-sources and commons-net-examples-3.3 files in the android project's lib, and also included the java files and folders from

commons-net-3.3-src | src | main | java | org | apache | commons | net folder to the Android project's src folder by creating the folders org | apache | commons | net.

I could not figured out what is the problem.

Can any one help me in this regard.

The same server can be accessed with a comercial ftp client.

Thanks in advance.

解决方案

I've tried your code and it works ok. Based on the response code you're getting I assume the connection to the server has not been even established. Make sure you have granted your app with INTERNET permission in the manifest file:

EDIT: From the exception stack trace it's clear, that you're trying to execute this code from a main thread. In Android 3.0 (Honeycomb) or later you're not permitted to execute any network call in UI thread by default. The reason is simple: network call may block for an indefinite amount of time, effectively freezing application UI.

You have two options how to fix it:

Lazy way - Disable therad check for network calls:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()

.permitAll()

.build();

StrictMode.setThreadPolicy(policy);

You can always enable thread checks again using

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()

.detectAll()

.penaltyLog()

.penaltyDeath()

.build();

StrictMode.setThreadPolicy(policy);

Proper way - execute FTP calls from a background worker thread using AsyncTask. Take a look here for examples and comprehensive explanation about AsyncTasks.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值