我无法在 Java-Android 中使用 FTPClient 连接到我的 FTP 服务器的主机

I can't connect to host of my FTP server with FTPClient in Java-Android

简介

你好,

对于一个项目,我必须在 FTP 服务器(端口:21)上连接一个 android 设备。

经过研究,我找到了一个允许 android 设备和 FTP 服务器之间连接的软件包。

所以我按照在 Internet 上找到的示例进行操作,并在我的 Raspberry Pi 中创建了我的 FTP 服务器,在我的设备的同一网络中。

但是我的应用程序无法连接到它。如应用程序的屏幕截图所示:

尝试解决问题的研究

密码

FTPModel.java

package com.example.erwan.ftplearn;

import android.util.Log;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;

import java.io.IOException;
import java.net.InetAddress;

public class FTPModel {
    public FTPClient mFTPClient = null;

    public boolean connect(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
            boolean status = mFTPClient.login(username, password);

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

        } catch (Exception e) {
            Log.i("testConnection", "Error: could not connect to host " + host);

        }
        return false;
    }
}

MainActivity.java

package com.example.erwan.ftplearn;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.adeel.library.easyFTP;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FTPModel mymodel = new FTPModel();
        boolean co = mymodel.connect("192.168.1.25", "xxxxx", "xxxxx", 21);

        TextView statut = findViewById(R.id.connexionstatut);
        if(co)
        {
            statut.setText("Connecter !");
        }
        else
        {
            statut.setText("Erreur de connexion !");
        }


    }
}

输出

2019-04-06 19:26:31.876 16869-16869/? I/testConnection: Error: could not connect to host 192.168.1.25
Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1460)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:356)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:201)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:183)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
        at java.net.Socket.connect(Socket.java:616)
        at org.apache.commons.net.SocketClient.connect(SocketClient.java:182)
        at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
        at com.example.erwan.ftplearn.FTPModel.connect(FTPModel.java:21)
        at com.example.erwan.ftplearn.MainActivity.onCreate(MainActivity.java:22)
        at android.app.Activity.performCreate(Activity.java:7383)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)

我想,这个错误是因为必须用另一个线程连接到 FTP 服务器,但我不知道如何连接。

我找到问题了! FTP 服务器上的连接必须在异步任务中建立!

FTP客户端不喜欢在主线程中工作,所以我们必须在另一个线程中工作。

package com.example.erwan.ftplearn;

import android.os.AsyncTask;
import android.util.Log;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;

import java.io.IOException;
import java.net.InetAddress;

public class FTPModel {
    public FTPClient mFTPClient = null;

    public boolean connect(String host, String username, String password, int port)
    {
        try
        {
            return new asyncConnexion(host, username, password, port).execute().get();
        }
        catch (Exception e)
        {
            return false;
        }
    }

    public class asyncConnexion extends AsyncTask<Void, Void, Boolean>
    {
        private String host;
        private String username;
        private String password;
        private int port;

        asyncConnexion(String host, String username, String password, int port)
        {
            this.host = host;
            this.password = password;
            this.port = port;
            this.username = username;
        }


        @Override
        protected Boolean doInBackground(Void... voids) {
            try {

                mFTPClient = new FTPClient();
                // connecting to the host
                mFTPClient.connect(host, port);

                // now check the reply code, if positive mean connection success
                boolean status = mFTPClient.login(username, password);

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

            } catch (Exception e) {
                Log.i("testConnection", "Error: could not connect to host " + host);
                e.printStackTrace();

            }
            return false;
        }
    }
}