1

I am trying to access files on an FTP server in Android.

For some reason whenever I import any external jar such as commons-net or ftp4j my app crashes on run. I have verified this by commenting out all lines that refer to the jars and the app runs properly.

I want to access data in a text file on an FTP server and print it out to a TextView. I have been stuck on this all day. How do I fix this? Permissions including in manifest are internet and external storage.

package com.testing.pack;

import java.io.BufferedInputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.http.client.HttpClient;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class TestingActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */

    TextView tv;
    Button go;
    HttpClient client;
    FTPClient ftp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.tvTry);
        Typeface face = Typeface.createFromAsset(getAssets(),
                "Roboto-LightItalic.ttf");
        tv.setTypeface(face);
        go = (Button) findViewById(R.id.bGO);
        go.setOnClickListener(this);

        try {
                        //Attempt using commons-net


            FTPClient ftpClient = new FTPClient();
            ftpClient.connect("ftp://........./");
            ftpClient.login(".......", ".....");
            ftpClient.changeWorkingDirectory("......");
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            BufferedInputStream buffIn=null;
            buffIn=new BufferedInputStream(new FileInputStream(file));
            ftpClient.enterLocalPassiveMode();
            ftpClient.storeFile("test.txt", buffIn);
            buffIn.close();
            ftpClient.logout();
            ftpClient.disconnect();

                        //Attempt using ftp4j

                        //FTPClient ftp = new FTPClient();
            //ftp.connect("........");
            //ftp.login(".......", ".....");
            //ftp.changeDirectoryUp();
            //ftp.changeDirectory(".....");
            //ftp.changeDirectory("public_ftp");
            //ftp.download("test.txt", new java.io.File("....."));
        } catch (Exception e) {

        }

    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        getData ex = new getData();
        try {
            tv.setText(ex.getInternetData());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

1 Answers1

1

Are you using Eclipse to add the jars? Instead create a libs folder and copy the jars into it.

Here is a similar question: How can I use external JARs in an Android project?

Community
  • 1
  • 1
Ryan
  • 1,135
  • 1
  • 13
  • 28
  • yes it worked, thanks. One question, why did jars not get added by clicking project name, properties...add external jar...isnt that supposed to do the same thing? – Saagar Gadhok Jun 08 '12 at 14:19
  • I was not sure and found this answer by Russ Bateman. [link](http://stackoverflow.com/questions/3642928/adding-a-library-jar-to-an-eclipse-android-project). If this answer solved your problem you can click the checkbox so everyone knows this question is answered. – Ryan Jun 08 '12 at 14:22