0

Introduction

Hello,

For a project I must connect an android device at an FTP server (Port: 21).

After research I have found a package for permit a connection between an android device and an FTP server.

So I have followed an example find in Internet, and I have created my FTP server in my Raspberry Pi in the same network of my device.

But my application can't connect to it. As show in this screenshot of the application:

enter image description here

The research for try to fix the problem

  • In the begin, I have try to use easyFTP an another package, but I had the same problem.

  • After put before the IP address of the server ftp:// but without success.

  • I have try to connect at my FTP server with the software Filezilla with my personnal computer, and it work.

Filezilla

The code

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 !");
        }


    }
}

The output

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)

I thinks, this error is here because the connexion to the FTP server must be make with a another thread, but I don't know how make it.

Community
  • 1
  • 1
CookieHacker
  • 103
  • 2
  • 11
  • I'm pretty sure your code is getting [`NetworkOnMainThreadException`](https://stackoverflow.com/q/6343166/850848) --- But you do not even log the exception you were getting, so how did you expect to debug the problem? – Martin Prikryl Apr 07 '19 at 05:25
  • I hadn't seen this error, and when I wanted get se message of the exception I have a ```NullPointerException```. It's after when I have see all the log, that I have find the ```NetworkOnMainThreadException``` with a friend. – CookieHacker Apr 07 '19 at 12:19

1 Answers1

2

I have found the problem! The connexion at an FTP server must be making in a async task!

FTPClient doesn't like to work in the main thread, so we must make work in an another thread.

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;
        }
    }
}
CookieHacker
  • 103
  • 2
  • 11