问题描述
我是新来的Android和Java编程。我写下面code连接到FTP服务器
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());
}
}
当我运行这个code我没有得到任何具体的错误,但该程序没有执行mFTPClient.connect方法并跳转到例外的一部分,我得到空作为例外的getMessage和0的回复code。
我有导入所有必需的文件,如
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;
我使用Eclipse和ADT构建v22.0.1-685705。
我也包括公网-3.3,公共净3.3来源和公共净例子-3.3的Android项目的lib文件,也包括了Java文件和文件夹
公网-3.3-SRC | SRC |主要| java的|组织|阿帕奇|公共|网络文件夹复制到Android项目的src文件夹中创建文件夹组织|阿帕奇|公共|净。
我也没有想出什么问题。
任何一个可以帮助我在这方面。
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 fromcommons-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.
相同的服务器可以与罗科梅FTP客户端来访问。
The same server can be accessed with a comercial ftp client.
先谢谢了。
推荐答案
我试过你的code和它工作正常。根据你得到的响应code我承担服务器的连接尚未建立连。请确保您已经授予INTERNET权限您的应用程序清单文件:
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:
<uses-permission android:name="android.permission.INTERNET" />
编辑:从异常堆栈跟踪很明显,那你想从一个主线程执行此code。在Android 3.0的(蜂窝)或更高版本你不得在默认情况下执行的UI线程任何网络电话。原因很简单:网络呼叫可能会阻塞无限期地,有效地冻结应用程序UI。
你有两个选择如何解决它:
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:
偷懒的办法 - 禁用therad检查网络呼叫:
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);
有道 - 执行使用AsyncTask的从后台辅助线程调用FTP。看看例子和有关AsyncTasks COM prehensive解释
Proper way - execute FTP calls from a background worker thread using AsyncTask. Take a look here for examples and comprehensive explanation about AsyncTasks.
这篇关于Java的应用程序ftpclient不连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!