0

I have two functions that access the internet on the APP start. I've tried to use this post as a reference in order to have the popup dialog while my content loads.

The two functions I would use are:

getImage(); //Gets an image from the internet for an imageview
getJson();  //Where the app goes an parses a JSON object for a lazy load listview.

The problem I'm encountering with the post I referenced above is that I try to make the task return null but it causes the app to crash when I do this. So I have this:

private class DownloadTask extends AsyncTask<String, Void, Object> {
protected Object doInBackground(String... args) {
    Log.i("MyApp", "Background thread starting");

    try {
        ImageView i = (ImageView) findViewById(R.id.currdoodlepic);
        Bitmap bitmap = BitmapFactory
                .decodeStream((InputStream) new URL(imageURL)
                        .getContent());
        i.setImageBitmap(bitmap);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    getJson("all");

    return "replace this with your data object";
}  

I'm not sure what to return.

Community
  • 1
  • 1
Nick
  • 9,285
  • 33
  • 104
  • 147

2 Answers2

0

The type return of the method doInBackground depend of what you need at the post execution :

void postExecute(Object result); // AsyncTask method

Parameter "result" is the return value of doInBackground. So if you need nothing you return NULL.

damson
  • 2,635
  • 3
  • 21
  • 29
0

I found the exact answer here. Here is the code:

ImageView mChart = (ImageView) findViewById(R.id.imageview);
String URL = "http://www...anything ...";

mChart.setTag(URL);
new DownloadImageTask.execute(mChart);

The Task class:

public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {

ImageView imageView = null;

@Override
protected Bitmap doInBackground(ImageView... imageViews) {
    this.imageView = imageViews[0];
    return download_Image((String)imageView.getTag());
}

@Override
protected void onPostExecute(Bitmap result) {
    imageView.setImageBitmap(result);
}


private Bitmap download_Image(String url) {
   ...
}
Community
  • 1
  • 1
Nick
  • 9,285
  • 33
  • 104
  • 147