简体   繁体   中英

Android Network Connection Crashing

I have a very simple program I'm using to connect to the internet and retrieve an image as an asynchronous task (this is really a learning step to get it to work with an XML pull). However, I have two interesting things happening. When I run the app on my phone (Samsung Galaxy 2), with the asynchronous code commented out, I get a white screen, the connection errors appear like they should, and all is well (except for it not connecting). When I try to run the asynchronous code, my background stays on my phone, icons disappear, and i get an error that the application has stopped working. What am I doing wrong?

Code for the 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);
}

}

Code for calling it:

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

}

Code for the stuff it calls:

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!

Hope following code will work. also don't forget to set internet permission.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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