Mudasser Naseer
Mudasser Naseer

Reputation: 31

Java ftpclient application doesn't connect

I am new to android and java programing. I write following code to connect to ftp server

public class NewMainActivity extends Activity {
TextView Display;
//declare a public FTP client object.
public FTPClient mFTPClient = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_main);
    final TextView Display = (TextView) findViewById(R.id.tvResults);

    try {
         mFTPClient = new FTPClient();

          // connecting to the host
         mFTPClient.connect("www.filegenie.com",21); 

          // now check the reply code, if positive mean connection success
          if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
              // login using username & password
              boolean status = mFTPClient.login("user", "MyPassword");

              // Set File Transfer Mode

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

              Display.setText("Correct"+ String.valueOf(status)); //return status;
          }

        }catch(Exception e) {
             Display.setText(" Exception message is = "+e.getMessage()+" Reply code = "+mFTPClient.getReplyCode());

        }

}

When I run this code I get no specific error but the program didn't execute mFTPClient.connect method and jumps to the 'Exception' part and I get "null" as exception getMessage and "0" as ReplyCode. I have import all the required files such as

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

I am using eclipse and ADT Build v22.0.1-685705. I have also included commons-net-3.3, commons-net-3.3-sources and commons-net-examples-3.3 files in the android project's lib, and also included the java files and folders from commons-net-3.3-src | src | main | java | org | apache | commons | net folder to the Android project's src folder by creating the folders org | apache | commons | net. I could not figured out what is the problem. Can any one help me in this regard.

The same server can be accessed with a comercial ftp client.

Thanks in advance.

Upvotes: 3

Views: 6102

Answers (1)

Jk1
Jk1

Reputation: 11443

I've tried your code and it works ok. Based on the response code you're getting I assume the connection to the server has not been even established. Make sure you have granted your app with INTERNET permission in the manifest file:

<uses-permission android:name="android.permission.INTERNET" /> 

EDIT: From the exception stack trace it's clear, that you're trying to execute this code from a main thread. In Android 3.0 (Honeycomb) or later you're not permitted to execute any network call in UI thread by default. The reason is simple: network call may block for an indefinite amount of time, effectively freezing application UI. You have two options how to fix it:

Lazy way - Disable therad check for network calls:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                                     .permitAll()
                                     .build();   
StrictMode.setThreadPolicy(policy);

You can always enable thread checks again using

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                                     .detectAll()
                                     .penaltyLog()
                                     .penaltyDeath()
                                     .build();
StrictMode.setThreadPolicy(policy);

Proper way - execute FTP calls from a background worker thread using AsyncTask. Take a look here for examples and comprehensive explanation about AsyncTasks.

Upvotes: 4

Related Questions