Ftp connect error

i'm using this code and it works well on Samsung Galaxy Tab1 with Android v2.3.7

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public FTPClient mFTPClient = null;

mFTPClient = new FTPClient();
mFTPClient.connect(host, port); 
.........

now i0've installed my app on Samsung Galaxy Tab2 with Andorid v4.0.3 but i receive this error when i try to connect:

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to bind to service com.quickorder.Ftp@412e4bd8 with Intent { cmp=com.quickorder/.Ftp (has extras) }: java.lang.NullPointerException: println needs a message at android.app.ActivityThread.handleBindService(ActivityThread.java:2304) at android.app.ActivityThread.access$1700(ActivityThread.java:128) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4514) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException: println needs a message at android.util.Log.println_native(Native Method) at android.util.Log.d(Log.java:155) at com.quickorder.Ftp.ftp_get(Ftp.java:131) at com.quickorder.Ftp.onBind(Ftp.java:66) at android.app.ActivityThread.handleBindService(ActivityThread.java:2291) ... 10 more

    public void ftp_get(String $name_in, String $name_fi){

    try {

        mFTPClient = new FTPClient();
        mFTPClient.connect(host, port);   
        //String $mess =  mFTPClient.getReplyString();
       // System.out.println($mess);
        if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {


            boolean status = mFTPClient.login(user, pass);
            //String $mess =  mFTPClient.getReplyString();
            //System.out.println($mess);

            mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
            mFTPClient.enterLocalPassiveMode();

            String srcFilePath = "QuickOrder/"+ag1+$name_in;
            String desFilePath = $name_fi;
            try {

                 try {

                     File sdcard = Environment.getExternalStorageDirectory();
                     File file = new File(sdcard, $name_fi);

                     BufferedOutputStream desFileStream = new BufferedOutputStream(new FileOutputStream("/sdcard/"+desFilePath),8*1024);
                     boolean sts = mFTPClient.retrieveFile(srcFilePath, desFileStream);
                     //String $mess =  mFTPClient.getReplyString();
                     //System.out.println($mess);
                     desFileStream.close();        

                 }catch(Exception e) {
                     String log = e.getMessage();
                     Log.d("Errore: ", log);
                 }
            } catch (Exception e) {
                }
            //System.out.println("file prelevato");
            mFTPClient.logout();
            mFTPClient.disconnect();
        }

    }catch(Exception e) {
        String log = e.getMessage(); //**this is LINE 131**
        Log.d("Errore: ", log);

    }


}

e.printStackTrace()

04-01 11:56:35.208: W/System.err(9154): android.os.NetworkOnMainThreadException
04-01 11:56:35.208: W/System.err(9154): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
04-01 11:56:35.208: W/System.err(9154):     at    libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
04-01 11:56:35.208: W/System.err(9154):     at  libcore.io.IoBridge.connectErrno(IoBridge.java:127)
04-01 11:56:35.208: W/System.err(9154):     at libcore.io.IoBridge.connect(IoBridge.java:112)
04-01 11:56:35.208: W/System.err(9154):     at  java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-01 11:56:35.208: W/System.err(9154):     at  java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
04-01 11:56:35.208: W/System.err(9154):     at java.net.Socket.connect(Socket.java:842)
04-01 11:56:35.208: W/System.err(9154):     at  org.apache.commons.net.SocketClient.connect(SocketClient.java:176)
04-01 11:56:35.208: W/System.err(9154):     at com.quickorder.Ftp.ftp_get(Ftp.java:91)
04-01 11:56:35.208: W/System.err(9154):     at com.quickorder.Ftp.onBind(Ftp.java:67)
04-01 11:56:35.208: W/System.err(9154):     at android.app.ActivityThread.handleBindService(ActivityThread.java:2291)
04-01 11:56:35.215: W/System.err(9154):     at android.app.ActivityThread.access$1700(ActivityThread.java:128)
04-01 11:56:35.215: W/System.err(9154):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
04-01 11:56:35.215: W/System.err(9154):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 11:56:35.215: W/System.err(9154):     at android.os.Looper.loop(Looper.java:137)
04-01 11:56:35.215: W/System.err(9154):     at android.app.ActivityThread.main(ActivityThread.java:4514)
04-01 11:56:35.215: W/System.err(9154):     at java.lang.reflect.Method.invokeNative(Native Method)
04-01 11:56:35.215: W/System.err(9154):     at java.lang.reflect.Method.invoke(Method.java:511)
04-01 11:56:35.215: W/System.err(9154):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
04-01 11:56:35.215: W/System.err(9154):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-01 11:56:35.215: W/System.err(9154):     at dalvik.system.NativeStart.main(Native  Method)

Answers


You can't do networking operations in the main thread. Rewrite your code like that for example: (sorry for indentation)

public void ftp_get(String $name_in, String $name_fi){
AsyncTask< String, Integer, Boolean > task = new AsyncTask< String, Integer, Boolean >()
            {

                private Exception m_exception = null;
                @Override
                protected Boolean doInBackground( String... params )
                {
                    Boolean result = Boolean.FALSE;


        try {

        mFTPClient = new FTPClient();
        mFTPClient.connect(host, port);   
        //String $mess =  mFTPClient.getReplyString();
       // System.out.println($mess);
        if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {


            boolean status = mFTPClient.login(user, pass);
            //String $mess =  mFTPClient.getReplyString();
            //System.out.println($mess);

            mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
            mFTPClient.enterLocalPassiveMode();

            String srcFilePath = "QuickOrder/"+ag1+$name_in;
            String desFilePath = $name_fi;
            try {

                 try {

                     File sdcard = Environment.getExternalStorageDirectory();
                     File file = new File(sdcard, $name_fi);

                     BufferedOutputStream desFileStream = new BufferedOutputStream(new FileOutputStream("/sdcard/"+desFilePath),8*1024);
                     boolean sts = mFTPClient.retrieveFile(srcFilePath, desFileStream);
                     //String $mess =  mFTPClient.getReplyString();
                     //System.out.println($mess);
                     desFileStream.close();   

        result = Boolean.TRUE;     

                 }catch(Exception e) {
                     String log = e.getMessage();
                     Log.d("Errore: ", log);
                 }
            } catch (Exception e) {
                }
            //System.out.println("file prelevato");
            mFTPClient.logout();
            mFTPClient.disconnect();
        }

    }catch(Exception e) {
        String log = e.getMessage(); //**this is LINE 131**
        Log.d("Errore: ", log);

    }

                    return result;
                }

                protected void onPostExecute(Boolean result) {

                    if ( m_exception == null && result )
                    {
                        callback.onSuccess( "success!" );
                    }
                    else
                    {
                        callback.onFailure( "Connection failed." );
                    }
                }


            };

        task.execute( "" );
}

Need Your Help

How to get Javascript object from HTML element

javascript html css

I have 3 "dice" objects created from this custom constructor:

Render view contain django location field in template

django python-3.x django-templates postgis geodjango

I writing an app that permits users to add and edit pump station with his location in map.