Android SimpleFTP 上传致命异常

Android SimpleFTP Upload FatalException

我正在慢慢自学 Android 和 Java,我有这段代码通过 FTP 向 server/nas 提交文件。到目前为止,一切都很好,除了提交。单击按钮后,出现错误,并强制关闭。我可以通过 cmd、FileZilla 或 TinyFtp 访问 ftp 控制台。所以,我不认为是这样。我已经导入了 SimpleFTP 库。任何帮助,无论是直接代码修复还是只是告诉我我做错了什么,我都会找到它,将不胜感激。谢谢

这是我的代码:

public class LegendsSubmitActivity extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_legends_submit);

    Button submit = (Button)findViewById(R.id.submit_btn);
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                SimpleFTP ftp = new SimpleFTP();

                // Connect to an FTP server on port 21.
                //real info is there in code
                ftp.connect("xx", 21, "xx", "xx");

                // Set binary mode.
                ftp.bin();

                // Change to a new working directory on the FTP server.
                ftp.cwd("home/upload/");

                // Upload some files.
                ftp.stor(new File(Environment.getExternalStorageDirectory().getPath() + "/Car/" + "EstimateFiles.zip"));
                //ftp.stor(new File("comicbot-latest.png"));

                // You can also upload from an InputStream, e.g.
                //ftp.stor(new FileInputStream(new File("test.png")), "test.png");
                // ftp.stor(someSocket.getInputStream(), "blah.dat");

                // Quit from the FTP server.
                ftp.disconnect();
            } catch (IOException e) {
                // Jibble.
                System.err.println("You done effed up" + e.getMessage());
            }


        }
    });

 }
}

这是一个日志:

10-13 10:59:38.521 28069-28069/com.gabeogan.damageestimator D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator D/AndroidRuntime: Shutting down VM
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime: FATAL EXCEPTION: main
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime: Process: com.gabeogan.damageestimator, PID: 28069
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime: android.os.NetworkOnMainThreadException
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at libcore.io.IoBridge.connect(IoBridge.java:122)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at java.net.Socket.startupSocket(Socket.java:590)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at java.net.Socket.tryAllAddresses(Socket.java:128)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at java.net.Socket.<init>(Socket.java:178)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at java.net.Socket.<init>(Socket.java:150)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at org.jibble.simpleftp.SimpleFTP.connect(SimpleFTP.java:68)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at com.gabeogan.damageestimator.LegendsSubmitActivity.onClick(LegendsSubmitActivity.java:34)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at android.view.View.performClick(View.java:5254)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:21179)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6837)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
10-13 10:59:38.581 28069-28069/com.gabeogan.damageestimator E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

android.os.NetworkOnMainThreadException 与处理 UI(用户界面)的单个线程现在被阻塞等待网络结果(例如,可能会失败)这一事实有关。 ..

设备不允许这种行为,然后关闭程序以允许用户继续使用设备。

检查 AsyncTask 作为处理问题的方法。