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:
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.
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.