Q:

Connecting to FTP server using AsyncTask

I am quite new to android developing 
I have written the following code to connect my android to a ftp server
package com.example.test1;
import android.os. Bundle;
import android.app. Activity;
import android.util. Log;
import android.view. Menu;
import android.view. View;
import android.widget. Button;
import android.widget. Toast;

import org.apache.commons.net.ftp.*; 
public class MainActivity extends Activity {
    public FTPClient mFTPClient = null;
    Button but;
    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;
           }
       } catch(Exception e) {
       CharSequence c = ""+e;
           int d = Toast. LENGTH_SHORT;
           Toast toast = Toast.makeText(getApplicationContext(),c,d);
           toast.show();
       }

       return false;
    }
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        but=(Button)findViewById(R.id.button1);
        but.setOnClickListener(new View. OnClickListener() {

            @Override
            public void onClick(View v) {
                ftpConnect("127.0.0.1","userName","Password",21);
            }
        });
    }   
}

But this gives a networkOnMainThread Exception so after searching i found out i have to use AsyncTask but i have no idea how to implement it! 

A:

public void onClick(View v) {
  new AsyncTask() {
    publc Object doInBackground(Object...) {
      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;
        }
      } catch(Exception e) {
        return e;
      }
    }
  }

  public void onPostExecute(Object res) {
    if (res instanceof Exception) {
       int d = Toast. LENGTH_SHORT;
       Toast toast = Toast.makeText(getApplicationContext(),""+res,d);
       toast.show();
    }
  }

}.execute();

Another temporary 'workaround' is to set your android:targetSdkVersion="9" or below in AndroidManifest.xml, as this exception was introduced in API level 10.