通过我的Android应用程序连接到FTP服务器

时间:2012-09-28 15:29:43

标签: android apache

我搜索了很多内容并且我有一些方法来连接我的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服务器还可以。

1 个答案:

答案 0 :(得分:0)

因为njzk2说我把我的代码放在asynctasc中,我的问题解决了。谢谢每个人的身体