Skip to content
Advertisement

FTP connection java

I’m trying to upload the file to a server. What is the way for uploading a file to a server through FTP?

i wrote this class:

serverconnect.java:

import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.SocketClient;
import org.apache.commons.net.ftp.FTPClient;


public class serverconnection
{

    public FTPClient connectftp() 
    {
        FTPClient ftp = null;
        try {
            ftp.connect("ftp://ftp.drivehq.com/");
            ftp.login("zule", "*****");
        //  ftp.changeWorkingDirectory("/public");
        //  ftp.makeDirectory("200");
        } catch (SocketException en) {
            // TODO Auto-generated catch block
            en.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return ftp;
    }
}

and this is the mainActivity ( only the relevant code):

import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;



public class MainActivity extends Activity implements OnClickListener {
    Button scan;
    String contents;
    String format;
    TextView contentstext;
    TextView formattext;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //----------------------FTP-------------
        serverconnection ftpconnect =new serverconnection();
        FTPClient ftp=ftpconnect.connectftp();
        scan=(Button)findViewById(R.id.scanbutton);
        .....

and when i install the app on my phone i get an error: “unfortanly your app must stopp…”

the new code:

public class MainActivity extends Activity implements OnClickListener {
    Button scan;
    String contents;
    String format;
    TextView contentstext;
    TextView formattext;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //----------------------FTP-------------
        //serverconnection ftpconnect =new serverconnection();
        //SimpleFTP ftp=ftpconnect.connectftp();
        SimpleFTP ftp = new SimpleFTP();
        try {
            ftp.connect("market.bugs3.com", 21, "u884282808", "lionetwork1");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

the new logcat:

09-16 13:43:31.131: E/AndroidRuntime(1203): FATAL EXCEPTION: main
09-16 13:43:31.131: E/AndroidRuntime(1203): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.market/com.example.market.MainActivity}: android.os.NetworkOnMainThreadException
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.os.Looper.loop(Looper.java:137)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at java.lang.reflect.Method.invokeNative(Native Method)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at java.lang.reflect.Method.invoke(Method.java:511)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at dalvik.system.NativeStart.main(Native Method)
09-16 13:43:31.131: E/AndroidRuntime(1203): Caused by: android.os.NetworkOnMainThreadException
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at java.net.Socket.tryAllAddresses(Socket.java:108)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at java.net.Socket.<init>(Socket.java:177)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at java.net.Socket.<init>(Socket.java:149)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at org.jibble.simpleftp.SimpleFTP.connect(SimpleFTP.java:68)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at com.example.market.MainActivity.onCreate(MainActivity.java:31)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.app.Activity.performCreate(Activity.java:4465)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-16 13:43:31.131: E/AndroidRuntime(1203):     ... 11 more

Advertisement

Answer

Note: AsyncTask class was deprecated in API level 30. Please use java.util.concurrent instead.

The problem is the fact that you’re trying to make a network call on your main thread. Which is not allowed on Android 3.0 or higher.

You should solve this by calling the FTP server on a different thread. A good way to do this is to use AsyncTask:

private class FtpTask extends AsyncTask<Void, Void, FTPClient> {
     protected FTPClient doInBackground(Void... args) {
        serverconnection ftpconnect =new serverconnection();
        FTPClient ftp=ftpconnect.connectftp();
        return ftp;
     }

     protected void onPostExecute(FTPClient result) {
         Log.v("FTPTask","FTP connection complete");
         ftpClient = result;
         //Where ftpClient is a instance variable in the main activity
     }
 }

Then you can run this background thread using the following code in the main thread:

new FtpTask().execute();

EDIT:

If you want to pass parameters between the different methods, you can change the AsyncTask<Void, Void, Void> superclass initialization.

For example AsyncTask<String, Double, Integer> will make it possible to pass a String variable to the doInBackground method, keep track of progress using a double and use a integer as the result type(the result type is the return type of doInBackground, which will be sent to onPostExecute as a parameter).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement