我用谷歌搜索了很多,我找到了一些连接我的 ftp 服务器的方法:最好的方法是使用 apache 包。我正在使用此代码并从许多站点读取它,但不幸的是,此代码对我不起作用:
public class ApacheFTPClient extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FTPClient mFTP = new FTPClient();
try {
// Connect to FTP Server
mFTP.connect("192.168.1.110");
mFTP.login("user", "password");
mFTP.setFileType(FTP.BINARY_FILE_TYPE);
mFTP.enterLocalPassiveMode();
// Prepare file to be uploaded to FTP Server
File file = new File("/path/to/filetotranfer");
FileInputStream ifile = new FileInputStream(file);
// Upload file to FTP Server
mFTP.storeFile("filetotranfer",ifile);
mFTP.disconnect();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
但我得到以下错误:
09-28 18:48:12.018: E/AndroidRuntime(591): FATAL EXCEPTION: main
09-28 18:48:12.018: E/AndroidRuntime(591): android.os.NetworkOnMainThreadException
09-28 18:48:12.018: E/AndroidRuntime(591): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
09-28 18:48:12.018: E/AndroidRuntime(591): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:74)
09-28 18:48:12.018: E/AndroidRuntime(591): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
09-28 18:48:12.018: E/AndroidRuntime(591): at libcore.io.IoBridge.connect(IoBridge.java:112)
09-28 18:48:12.018: E/AndroidRuntime(591): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-28 18:48:12.018: E/AndroidRuntime(591): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
09-28 18:48:12.018: E/AndroidRuntime(591): at java.net.Socket.startupSocket(Socket.java:566)
09-28 18:48:12.018: E/AndroidRuntime(591): at java.net.Socket.tryAllAddresses(Socket.java:127)
09-28 18:48:12.018: E/AndroidRuntime(591): at java.net.Socket.<init>(Socket.java:177)
09-28 18:48:12.018: E/AndroidRuntime(591): at java.net.Socket.<init>(Socket.java:149)
09-28 18:48:12.018: E/AndroidRuntime(591): at org.jibble.simpleftp.SimpleFTP.connect(SimpleFTP.java:68)
09-28 18:48:12.018: E/AndroidRuntime(591): at my.time.tick2.Main$8.onClick(Main.java:1585)
09-28 18:48:12.018: E/AndroidRuntime(591): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:924)
09-28 18:48:12.018: E/AndroidRuntime(591): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
09-28 18:48:12.018: E/AndroidRuntime(591): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
09-28 18:48:12.018: E/AndroidRuntime(591): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
09-28 18:48:12.018: E/AndroidRuntime(591): at android.widget.AbsListView$1.run(AbsListView.java:3168)
09-28 18:48:12.018: E/AndroidRuntime(591): at android.os.Handler.handleCallback(Handler.java:605)
09-28 18:48:12.018: E/AndroidRuntime(591): at android.os.Handler.dispatchMessage(Handler.java:92)
09-28 18:48:12.018: E/AndroidRuntime(591): at android.os.Looper.loop(Looper.java:137)
09-28 18:48:12.018: E/AndroidRuntime(591): at android.app.ActivityThread.main(ActivityThread.java:4340)
09-28 18:48:12.018: E/AndroidRuntime(591): at java.lang.reflect.Method.invokeNative(Native Method)
09-28 18:48:12.018: E/AndroidRuntime(591): at java.lang.reflect.Method.invoke(Method.java:511)
09-28 18:48:12.018: E/AndroidRuntime(591): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-28 18:48:12.018: E/AndroidRuntime(591): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-28 18:48:12.018: E/AndroidRuntime(591): at dalvik.system.NativeStart.main(Native Method)
问题是什么?我的 ftp 服务器运行良好,我可以从任何地方使用匿名或服务器中的任何用户名访问它。我的 ftp 服务器没问题。