繁体   English   中英

Android网络连接崩溃

[英]Android Network Connection Crashing

我有一个非常简单的程序,用于连接到互联网并作为异步任务检索图像(这实际上是一个学习步骤,可使其与XML Pull一起使用)。 但是,我发生了两个有趣的事情。 当我在手机(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;
}

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!

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM