1
我搜索了很多,我有一些方法來連接我的FTP服務器:最好的方法是使用Apache包。我使用這個代碼,並從許多網站閱讀可惜這個代碼是不是爲我工作:通過我的Android應用連接到FTP服務器
public class ApacheFTPClient extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FTPClient mFTP = new FTPClient();
try {
// Connect to FTP Server
mFTP.connect("192.168.1.110");
mFTP.login("user", "password");
mFTP.setFileType(FTP.BINARY_FILE_TYPE);
mFTP.enterLocalPassiveMode();
// Prepare file to be uploaded to FTP Server
File file = new File("/path/to/filetotranfer");
FileInputStream ifile = new FileInputStream(file);
// Upload file to FTP Server
mFTP.storeFile("filetotranfer",ifile);
mFTP.disconnect();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
,但我得到以下誤差修改:
09-28 18:48:12.018: E/AndroidRuntime(591): FATAL EXCEPTION: main
09-28 18:48:12.018: E/AndroidRuntime(591): android.os.NetworkOnMainThreadException
09-28 18:48:12.018: E/AndroidRuntime(591): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
09-28 18:48:12.018: E/AndroidRuntime(591): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:74)
09-28 18:48:12.018: E/AndroidRuntime(591): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
09-28 18:48:12.018: E/AndroidRuntime(591): at libcore.io.IoBridge.connect(IoBridge.java:112)
09-28 18:48:12.018: E/AndroidRuntime(591): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-28 18:48:12.018: E/AndroidRuntime(591): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
09-28 18:48:12.018: E/AndroidRuntime(591): at java.net.Socket.startupSocket(Socket.java:566)
09-28 18:48:12.018: E/AndroidRuntime(591): at java.net.Socket.tryAllAddresses(Socket.java:127)
09-28 18:48:12.018: E/AndroidRuntime(591): at java.net.Socket.<init>(Socket.java:177)
09-28 18:48:12.018: E/AndroidRuntime(591): at java.net.Socket.<init>(Socket.java:149)
09-28 18:48:12.018: E/AndroidRuntime(591): at org.jibble.simpleftp.SimpleFTP.connect(SimpleFTP.java:68)
09-28 18:48:12.018: E/AndroidRuntime(591): at my.time.tick2.Main$8.onClick(Main.java:1585)
09-28 18:48:12.018: E/AndroidRuntime(591): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:924)
09-28 18:48:12.018: E/AndroidRuntime(591): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
09-28 18:48:12.018: E/AndroidRuntime(591): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
09-28 18:48:12.018: E/AndroidRuntime(591): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
09-28 18:48:12.018: E/AndroidRuntime(591): at android.widget.AbsListView$1.run(AbsListView.java:3168)
09-28 18:48:12.018: E/AndroidRuntime(591): at android.os.Handler.handleCallback(Handler.java:605)
09-28 18:48:12.018: E/AndroidRuntime(591): at android.os.Handler.dispatchMessage(Handler.java:92)
09-28 18:48:12.018: E/AndroidRuntime(591): at android.os.Looper.loop(Looper.java:137)
09-28 18:48:12.018: E/AndroidRuntime(591): at android.app.ActivityThread.main(ActivityThread.java:4340)
09-28 18:48:12.018: E/AndroidRuntime(591): at java.lang.reflect.Method.invokeNative(Native Method)
09-28 18:48:12.018: E/AndroidRuntime(591): at java.lang.reflect.Method.invoke(Method.java:511)
09-28 18:48:12.018: E/AndroidRuntime(591): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-28 18:48:12.018: E/AndroidRuntime(591): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-28 18:48:12.018: E/AndroidRuntime(591): at dalvik.system.NativeStart.main(Native Method)
是什麼問題?我的ftp服務器運行良好,我可以在任何地方使用匿名服務器或任何用戶名訪問服務器。我的FTP服務器沒問題。
NetworkOnMainThreadException +搜索框=解決方案。請在下一次詢問之前這樣做。 – njzk2
有什麼意思?我必須做什麼?你的意思是我把這些代碼放在asynctasc或類似的東西上? – aTa
嘗試在發佈新問題之前進行搜索。您必須從UI線程中分離任何下載操作以避免阻止您的應用程序,您必須將任務下載到AsynTask並在任務完成時更新ui。 –