首页新闻找找看学习计划

安卓FTP 上传出现异常

0
悬赏园豆:10 [已解决问题] 解决于 2015-06-20 21:27
 1 public String ftpUpload(String url, int port, String username,String password, String remotePath, String fileNamePath,String fileName) {
 2          FTPClient ftpClient = new FTPClient();
 3          FileInputStream fis = null;
 4          String returnMessage = "0";
 5          try {
 6              ftpClient.connect(url, port);//<--这里出错
 7              boolean loginResult = ftpClient.login(username, password);
 8              int returnCode = ftpClient.getReplyCode();
 9              if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功
10                  ftpClient.makeDirectory(remotePath);
11                  // 设置上传目录
12                  ftpClient.changeWorkingDirectory(remotePath);
13                  ftpClient.setBufferSize(1024);
14                  ftpClient.setControlEncoding("UTF-8");
15                  ftpClient.enterLocalPassiveMode();
16                          fis = new FileInputStream(fileNamePath + fileName);
17                  ftpClient.storeFile(fileName, fis);
18                  
19                  returnMessage = "1";   //上传成功         
20              } else {// 如果登录失败
21                  returnMessage = "0";
22                  }
23                       
24     
25          } catch (IOException e) {
26              e.printStackTrace();
27              throw new RuntimeException("FTP客户端出错!", e);
28          } finally {
29              //IOUtils.closeQuietly(fis);
30          try {
31              ftpClient.disconnect();
32          } catch (IOException e) {
33                  e.printStackTrace();
34                  throw new RuntimeException("关闭FTP连接发生异常!", e);
35              }
36          }
37          return returnMessage;
38      }

运行的时候LOGCAT在第6行出现异常,已经添加了

 <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

权限

FTP服务器正常运行,电脑和手机上的andftp可以连上FTP

 

LOGCAT错误信息如下

01-26 12:18:03.222: E/PhonePolicy(6973): Could not preload class for phone policy: com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
01-26 12:18:10.342: E/AndroidRuntime(6973): FATAL EXCEPTION: main
01-26 12:18:10.342: E/AndroidRuntime(6973): android.os.NetworkOnMainThreadException
01-26 12:18:10.342: E/AndroidRuntime(6973): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
01-26 12:18:10.342: E/AndroidRuntime(6973): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
01-26 12:18:10.342: E/AndroidRuntime(6973): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
01-26 12:18:10.342: E/AndroidRuntime(6973): at libcore.io.IoBridge.connect(IoBridge.java:112)
01-26 12:18:10.342: E/AndroidRuntime(6973): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-26 12:18:10.342: E/AndroidRuntime(6973): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
01-26 12:18:10.342: E/AndroidRuntime(6973): at java.net.Socket.connect(Socket.java:832)
01-26 12:18:10.342: E/AndroidRuntime(6973): at org.apache.commons.net.SocketClient.connect(SocketClient.java:168)
01-26 12:18:10.342: E/AndroidRuntime(6973): at org.apache.commons.net.SocketClient.connect(SocketClient.java:189)
01-26 12:18:10.342: E/AndroidRuntime(6973): at com.example.ftptest.MainActivity.ftpUpload(MainActivity.java:56)
01-26 12:18:10.342: E/AndroidRuntime(6973): at com.example.ftptest.MainActivity$1.onClick(MainActivity.java:38)
01-26 12:18:10.342: E/AndroidRuntime(6973): at android.view.View.performClick(View.java:4191)
01-26 12:18:10.342: E/AndroidRuntime(6973): at android.view.View$PerformClick.run(View.java:17229)
01-26 12:18:10.342: E/AndroidRuntime(6973): at android.os.Handler.handleCallback(Handler.java:615)
01-26 12:18:10.342: E/AndroidRuntime(6973): at android.os.Handler.dispatchMessage(Handler.java:92)
01-26 12:18:10.342: E/AndroidRuntime(6973): at android.os.Looper.loop(Looper.java:137)
01-26 12:18:10.342: E/AndroidRuntime(6973): at android.app.ActivityThread.main(ActivityThread.java:4963)
01-26 12:18:10.342: E/AndroidRuntime(6973): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 12:18:10.342: E/AndroidRuntime(6973): at java.lang.reflect.Method.invoke(Method.java:511)
01-26 12:18:10.342: E/AndroidRuntime(6973): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
01-26 12:18:10.342: E/AndroidRuntime(6973): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
01-26 12:18:10.342: E/AndroidRuntime(6973): at dalvik.system.NativeStart.main(Native Method)

風过无痕的主页 風过无痕 | 初学一级 | 园豆:13
提问于:2015-01-26 12:20
< >
分享
最佳答案
1

 android.os.NetworkOnMainThreadException

这个操作不能在主线程执行,要在子线程内进行。你开个Thread来执行上传操作,通过Hanlder执行上传结果回调

收获园豆:10
林J | 菜鸟二级 |园豆:202 | 2015-02-05 15:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册