0

最终,我的目标是获得一个从 URL 接收 XML 文件、解析它并显示相关信息的 Android 应用程序(嗯,最终目标比这复杂得多,但这是当前目标)。我正在使用 Wei-Meng Lee 的“开始 Android 应用程序开发”中的示例,并且我已经注意到本书的示例代码中存在一些错误(特别是一个错误名称的变量,如果 Eclipse 没有指出它,我会错过它完全地)。

然而,目前,该应用程序无法连接到互联网,尽管有权限,可以访问(3g、4g 和 wifi 都已测试),并且能够在板载浏览器中访问测试 URL。

以下是相关的代码片段。我做错什么了吗?(注意:我在模拟器和 Galaxy S2 上都进行了测试)

private InputStream OpenHttpConnection(String urlString)
throws IOException
{
    InputStream in = null;
    int response = 01;

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))
        throw new IOException("Not an HTTP connection");
    try{
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("Get");
        httpConn.connect();
        response = httpConn.getResponseCode();
        if (response == HttpURLConnection.HTTP_OK){
            in = httpConn.getInputStream();
        }
    }
    catch (Exception ex)
    {
        throw new IOException("Error connecting");
    }
    return in;
}

我尝试了不同的 URL,在使用它们之前检查我是否可以在手机的浏览器中连接到它们中的每一个。模拟器也没有运气。

更新:利用以下内容的异步代码:

private class BackgroundTask extends AsyncTask
<String, Void, Bitmap> {
    protected Bitmap doInBackground(String... url){
        // download an image
        Bitmap bitmap = DownloadImage(url[0]);
        return bitmap;
    }
}

protected void onPostExecute(Bitmap bitmap) {
    ImageView img = (ImageView) findViewById(R.id.img);
    img.setImageBitmap(bitmap);
}

上述方法调用:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new BackgroundTask().execute("http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif");

}
4

1 回答 1

1

您想将 AsyncTask 用于需要网络连接的任何事情。您可以按如下方式设置异步:(这需要一个字符串作为参数并返回一个 InputStream)

public class OpenHttpConnection extends AsyncTask<String, Void, InputStream> {

    @Override
    protected String doInBackground(String... params) {
      String urlstring = params[0];
      InputStream in = null;
      int response = 01;

      URL url = new URL(urlString);
      URLConnection conn = url.openConnection();

      if (!(conn instanceof HttpURLConnection))
        throw new IOException("Not an HTTP connection");
      try{
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("Get");
        httpConn.connect();
        response = httpConn.getResponseCode();
        if (response == HttpURLConnection.HTTP_OK){
          in = httpConn.getInputStream();
        }
      }
      catch (Exception ex)
      {
        throw new IOException("Error connecting");
      }
      return in;

    }

}

然后你可以像这样调用/运行你 Async 。

OpenHttpConnection connection = new OpenHttpConnection().execute("http://YourURL.com");
InputStream is = connection.get();
于 2013-08-16T21:00:04.457 回答