Sample code for 30+ languages & platforms
Android™

Control How FTP Directory Listings Are Requested

See more FTP Examples

Demonstrates the AllowMlsd, PreferNlst, and ListOption properties, which control which listing command Chilkat uses and what options it passes.

Background: FTP has three listing commands with different trade-offs. MLSD is machine-readable and standardized, so AllowMlsd (on by default) prefers it when the server supports it. When it must fall back to the legacy commands, PreferNlst chooses names-only NLST over the fuller LIST, and ListOption appends flags such as -a (to include hidden files) literally to LIST. Together they adapt listing to a particular server's capabilities and conventions.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    CkFtp2 ftp = new CkFtp2();

    ftp.put_Hostname("ftp.example.com");
    ftp.put_Username("myFtpLogin");

    //  AllowMlsd (default true) requests machine-readable MLSD listings when the server advertises
    //  support.  MLSD is preferred because its format is standardized.
    ftp.put_AllowMlsd(true);

    //  PreferNlst (default false) chooses NLST over LIST when a legacy listing is needed and MLSD is
    //  unavailable.  NLST returns only names.
    ftp.put_PreferNlst(false);

    //  ListOption is appended literally to a legacy LIST command; "-a" sends "LIST -a" to include
    //  hidden files on Unix servers.
    ftp.put_ListOption("-a");

    //  Normally you would not hard-code the password in source.  You should instead obtain it
    //  from an interactive prompt, environment variable, or a secrets vault.
    ftp.put_Password("myPassword");

    success = ftp.Connect();
    if (success == false) {
        Log.i(TAG, ftp.lastErrorText());
        return;
        }

    int n = ftp.GetDirCount();
    Log.i(TAG, "Entries: " + String.valueOf(n));

    success = ftp.Disconnect();
    if (success == false) {
        Log.i(TAG, ftp.lastErrorText());
        return;
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}