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

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

问题描述

我创建了一个Android项目上的2.3.3,并试图在移动2.3.3,一切工作正常。它没有移动4工作,所以我重新建为安卓4.0,但我有同样的问题。

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.

这是在code:

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的。如果你是,然后张贴在code和登录猫也。其它操作的一些例子,ICS和蜂窝状不会让你在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连接(即新的Socket())。
  • 在HTTP请求(即了HTTPClient和HttpURLConnection类)。
  • 在试图连接到远程MySQL数据库。
  • 下载文件(即Downloader.downloadFile())。

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

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