【问题标题】:Android FTP connection problems [duplicate]Android FTP连接问题[重复]
【发布时间】:2015-06-22 10:48:27
【问题描述】:

嗨,我已经尝试了两天来获得一个简单的 ftp 连接来传输一个小的 xml 文件。我尝试了很多不同的代码示例,但似乎都给出了相同的错误。

主 FTP 类代码:

public class MyFTPClientFunctions {

    public FTPClient mFTPClient = null;

    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);

                mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
                        mFTPClient.enterLocalPassiveMode();
                			return status;
            }

        //Log.d(TAG, "Error: could not connect to host " + host);
        } catch (SocketException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    private Context getApplicationContext() {
        return null;
    }


}

要发送的主要活动代码

send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //set up FTP file transfer here

                MyFTPClientFunctions ftpSend = new MyFTPClientFunctions();

                ftpSend.ftpConnect("xxxxxx.asuscomm.com","admin","xxxxxxxxxx",21);

            }

LogCat 消息

06-22 12:09:21.460  17329-17329/com.example.rats.moham_2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.rats.moham_2, PID: 17329
    android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
            at java.net.InetAddress.getByName(InetAddress.java:305)
            at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
            at com.example.rats.moham_2.MyFTPClientFunctions.ftpConnect(MyFTPClientFunctions.java:25)
            at com.example.rats.moham_2.MainActivity$3.onClick(MainActivity.java:155)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

【问题讨论】:

  • 获取异常名称和 GOOGLE ...

标签: java android ftp


【解决方案1】:

如果您在主线程上使用网络,通常会抛出此异常。 请使用异步任务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 2021-12-02
    • 2012-08-23
    • 2021-09-14
    • 1970-01-01
    相关资源
    最近更新 更多