我正在开发一个将文件上传到FTP服务器的应用程序,但它无法连接到服务器。它没有显示任何错误,但已停止运行。我还包括了互联网许可。 我的代码段以连接并上传文件
FTPClient ftpClient =新的FTPClient(); //显示FTPClient @ 5524
FileInputStream fileInputStream = null;
try {
ftpClient.connect("myipaddress",21);
if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
boolean s = ftpClient.login("username","password");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
if(!s){
Toast.makeText(getApplicationContext(),"Not able to login",Toast.LENGTH_LONG).show();
}
}
else {
Toast.makeText(getApplicationContext(),"Can't Connect",Toast.LENGTH_LONG).show();
}
ftpClient.changeWorkingDirectory("path for directory");
fileInputStream = new FileInputStream(new File(Environment.getExternalStorageDirectory() + "/" + folder + "/" + selectedfile));
boolean r = ftpClient.storeFile(selectedfile, fileInputStream);
if (r) Log.v("uploade result ","Succedded");
fileInputStream.close();
ftpClient.logout();
ftpClient.disconnect();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我的日志文件
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vaidehi.test, PID: 17779
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1452)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:355)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:357)
at java.net.Socket.connect(Socket.java:616)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:163)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:254)
at com.example.vaidehi.bata_test.MainActivity.Local_Server(MainActivity.java:174)
at com.example.vaidehi.bata_test.MainActivity$3.onClick(MainActivity.java:156)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:171)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6543)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)