Android错误NetworkOnMainThreadException

时间:2016-09-03 23:09:50

标签: java android network-programming

您好我尝试连接FTP设备。当我只使用Java编写代码时,它可以工作。

但随着Android Framework的发布工作。我记得e Exception android.os.NetworkOnMainThreadException

所以我创建了一个StrictMode.ThreadPolicy,但它没有帮助。

我的代码:

     private void connectFTP(String ip, String nameBox, String password) throws IOException {

        FTPClient client = new FTPClient();

        client.connect(ip);

        int reply = client.getReplyCode();

        if(!FTPReply.isPositiveCompletion(reply)) {
            client.disconnect();
            System.out.println("FTP server refused connection.");
            System.exit(0);
        }

        //after connecting to the server set the local passive mode
        client.enterLocalPassiveMode();
        client.login(nameBox, password);


        // Obtain a list of filenames in the current working
        // directory. When no file found an empty array will
        // be returned.
        String[] names = client.listNames();
        for (String name : names) {
            System.out.println("Name = " + name);
        }
    }

例外:

    java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                             at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                             at android.view.View.performClick(View.java:4856)
                                                                             at android.view.View$PerformClick.run(View.java:19956)
                                                                             at android.os.Handler.handleCallback(Handler.java:739)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:211)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5389)
                                                                             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:1020)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
                                                                          Caused by: java.lang.reflect.InvocationTargetException
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:372)
                                                                             at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                             at android.view.View.performClick(View.java:4856) 
                                                                             at android.view.View$PerformClick.run(View.java:19956) 
                                                                             at android.os.Handler.handleCallback(Handler.java:739) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                             at android.os.Looper.loop(Looper.java:211) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5389) 
                                                                             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:1020) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 
                                                                          Caused by: android.os.NetworkOnMainThreadException
                                                                             at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1156)
                                                                             at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
                                                                             at libcore.io.IoBridge.connectErrno(IoBridge.java:141)
                                                                             at libcore.io.IoBridge.connect(IoBridge.java:126)
                                                                             at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
                                                                             at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
                                                                             at java.net.Socket.connect(Socket.java:887)
                                                                             at org.apache.commons.net.SocketClient.connect(SocketClient.java:188)
                                                                             at org.apache.commons.net.SocketClient.connect(SocketClient.java:209)
                                                                             at org.apache.commons.net.SocketClient.connect(SocketClient.java:306)
                                                                             at com.example.eliseo.dreambox.LoginActivity.connectFTP(LoginActivity.java:66)
                                                                             at com.example.eliseo.dreambox.LoginActivity.sendMessage(LoginActivity.java:45)
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                             at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                             at android.view.View.performClick(View.java:4856) 
                                                                             at android.view.View$PerformClick.run(View.java:19956) 
                                                                             at android.os.Handler.handleCallback(Handler.java:739) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                             at android.os.Looper.loop(Looper.java:211) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5389) 
                                                                             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:1020) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 

0 个答案:

没有答案