-2

Hi I have been trying for two days to get a simple ftp connection to transfer a small xml file. I have tried lots of different examples of code, but all seem to give the same errors.

Main FTP class code:

public class MyFTPClientFunctions {

    public FTPClient mFTPClient = null;

    public boolean ftpConnect(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
            if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
                // login using username & password
                boolean status = mFTPClient.login(username, password);

                mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
                        mFTPClient.enterLocalPassiveMode();
                   return status;
            }

        //Log.d(TAG, "Error: could not connect to host " + host);
        } catch (SocketException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    private Context getApplicationContext() {
        return null;
    }


}

Main Activity code to send

send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //set up FTP file transfer here

                MyFTPClientFunctions ftpSend = new MyFTPClientFunctions();

                ftpSend.ftpConnect("xxxxxx.asuscomm.com","admin","xxxxxxxxxx",21);

            }

LogCat messages

06-22 12:09:21.460  17329-17329/com.example.rats.moham_2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.rats.moham_2, PID: 17329
    android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
            at java.net.InetAddress.getByName(InetAddress.java:305)
            at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
            at com.example.rats.moham_2.MyFTPClientFunctions.ftpConnect(MyFTPClientFunctions.java:25)
            at com.example.rats.moham_2.MainActivity$3.onClick(MainActivity.java:155)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 Answers1

0

This Exception is usualy thrown, if you are using the network on the main thread. Please use Async Tasks.

NewDeveloper
  • 120
  • 3
  • 15