0

我有一个非常简单的程序,用于连接到 Internet 并将图像作为异步任务检索(这实际上是一个学习步骤,可以让它与 XML 拉取一起工作)。但是,我有两件有趣的事情正在发生。当我在手机(Samsung Galaxy 2)上运行应用程序时,注释掉了异步代码,我得到一个白屏,连接错误看起来应该是这样,一切都很好(除了它没有连接)。当我尝试运行异步代码时,我的背景停留在我的手机上,图标消失,并且我收到应用程序停止工作的错误。我究竟做错了什么?

异步代码:

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");

}

它调用的东西的代码:

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

    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;
}

private Bitmap DownloadImage(String URL)
{
    Bitmap bitmap = null;
    InputStream in = null;
    try {
        in = OpenHttpConnection(URL);
        bitmap = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IOException e1) {
        Toast.makeText(this,  e1.getLocalizedMessage(), Toast.LENGTH_LONG).show();

        e1.printStackTrace();
    }
    return bitmap;
}

日志猫:

 08-20 09:13:27.294: E/AndroidRuntime(11130):   at com.example.networking.MainActivity$BackgroundTask.doInBackground(MainActivity.java:1)
 08-20 09:13:27.294: E/AndroidRuntime(11130):   at android.os.AsyncTask$2.call(AsyncTask.java:264)
 08-20 09:13:27.294: E/AndroidRuntime(11130):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 08-20 09:13:27.294: E/AndroidRuntime(11130):   ... 5 more
 08-20 09:13:50.469: V/InputMethodManager(11130): ABORT input: no handler for view!
4

1 回答 1

0

希望下面的代码可以工作。也不要忘记设置互联网权限。

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

}
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) {
    img.setImageBitmap(bitmap);
}

}
private Bitmap DownloadImage(String urlString)
{
    Bitmap bitmap = null;
    InputStream in = null;
    try {
 URL url = new URL(urlString);
        in=(InputStream) url.getContent();
    bitmap =BitmapFactory.decodeStream(in);
        in.close();
    } catch (IOException e1) {
        Toast.makeText(this,  e1.getLocalizedMessage(), Toast.LENGTH_LONG).show();

        e1.printStackTrace();
    }
    return bitmap;
}
于 2013-08-20T15:15:43.053 回答