Android应用程式无法在Android 4上运作 [英] Android app doesn't work on Android 4

查看:132
本文介绍了Android应用程式无法在Android 4上运作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在2.3.3上创建了一个Android项目,并在移动版2.3.3上进行了尝试,一切正常.在移动版4上无法正常运行,因此我为Android 4进行了重新构建,但是我遇到了同样的问题.

I created an Android project on 2.3.3 and tried it on mobile 2.3.3, everything works OK. It didn't work on mobile 4, so I re-built for Android 4, but I have the same problem.

这是代码:

public void FTP_Download(){
  String server = "192.168.1.135";
  int port = 21;
  String user = "pc1";
  String pass = "1551";
  FTPClient ftpClient = new FTPClient();
  try {
    ftpClient.connect(server, port);
    ftpClient.login(user, pass);
    ftpClient.enterLocalPassiveMode();
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    Toast.makeText(getBaseContext(), "download starting.",Toast.LENGTH_LONG).show();
    // APPROACH #1: using retrieveFile(String, OutputStream)
    String remoteFile1 = "i.xml";
    File downloadFile1 = new File("sdcard/i.xml");
    OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
    boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
    outputStream1.close();
    if (success) {
      Toast.makeText(getBaseContext(), "File #1 has been downloaded successfully.",Toast.LENGTH_LONG).show();
    }
  } catch (IOException ex) {
    System.out.println("Error: " + ex.getMessage());
    ex.printStackTrace();
  } finally {
    try {
      if (ftpClient.isConnected()) {
        ftpClient.logout();
        ftpClient.disconnect();
      }
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
}

注意:

我使用commons-net-3.1进行连接.

I use commons-net-3.1 to connect.

推荐答案

在上述Android版本2.3中,您无法从主UI线程启动互联网连接.相反,您应该使用 AsyncTask .我假设您没有使用AsyncTask.如果是,则发布代码并登录.ICS和HoneyComb不允许您在UI线程上执行的其他操作的一些示例是:(来自下面评论中发布的链接)-

In android version 2.3 above you can not start internet connection from main UI thread. Instead you should use AsyncTask. I assumed you are not using AsyncTask. If you are, then post the code and log cat also. Some examples of other operations that ICS and HoneyComb won't allow you to perform on the UI thread are:( from link posted in comment below ) -

  • 打开Socket连接(即new Socket()).
  • HTTP请求(即HTTPClient和HTTPUrlConnection).
  • 尝试连接到远程MySQL数据库.
  • 下载文件(即Downloader.downloadFile()).

这篇关于Android应用程式无法在Android 4上运作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆