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

查看:31
本文介绍了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 线程启动 Internet 连接.相反,您应该使用 AsyncTask.我假设您没有使用 AsyncTask.如果是,请发布代码并记录 cat .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天全站免登陆