Android SimpleFTP上传FatalException

时间:2015-10-13 16:05:12

标签: java android ftp fatal-error ftp-client

我正在慢慢地教自己Android和Java,我有这个代码,它通过FTP将文件提交到服务器/ 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$1.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)

1 个答案:

答案 0 :(得分:0)

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

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

检查AsyncTask是否可以解决问题。