Android网络连接崩溃

时间:2013-08-20 14:16:21

标签: android networking

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

Asynch的代码:

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

logcat的:

 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!

1 个答案:

答案 0 :(得分: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;
}