0

This is my first async task, which gets called first, it gets data from server and then onPostExecute it executes other async task, which downloads and sets image.

private class GetData extends AsyncTask<String, Void, Void> {
    private final HttpClient client = new DefaultHttpClient();
    private String content;
    private String error = null;

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... progress) {
    }

    @Override
    protected Void doInBackground(String... params) {
        try {
            HttpGet httpget = new HttpGet(params[0]);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            content = client.execute(httpget, responseHandler);
        } catch (ClientProtocolException e) {
            error = e.getMessage();
            cancel(true);
        } catch (IOException e) {
            error = e.getMessage();
            cancel(true);
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        if (error == null) {
            try {
                JSONObject dataDishes = new JSONObject(content);
                Log.d("DISHES", dataDishes.toString());

                ArrayList<DishModel> dishData = new ArrayList<DishModel>();
                for (int i = 0; i < 8; i++) {

                    DishModel model = new DishModel();

                    model.setName("Company " + i);
                    model.setDesc("desc" + i);
                    //TODO: set data img
                    new GetImage(model).execute("http://example.com/" + (i + 1) + ".png");
                    dishData.add(model);
                }

                ListView listAllDishes = (ListView) getView().findViewById(R.id.listView);

                DishRowAdapter adapterAllDishes = new DishRowAdapter(getActivity(),
                        R.layout.dish_row, dishData);

                listAllDishes.setAdapter(adapterAllDishes);

            } catch (JSONException e) {
                Log.d("DISHES", e.toString());
            }
        } else {
            Log.e("DISHES", error);
        }
    }
}

This is another async task, it downloads image and onPostExecute it sets image to passed model.

private class GetImage extends AsyncTask<String, Void, Void> {
    private DishModel model;
    private Bitmap bmp;

    public getImage(DishModel model) {
        this.model = model;
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... progress) {
    }

    @Override
    protected Void doInBackground(String... params) {
        try {
            URL url = new URL(params[0]);
            Log.d("DISHES", params[0]);
            try {
                bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            } catch (IOException e) {
                Log.d("DISHES", e.toString());
            }
        } catch (MalformedURLException e) {
            Log.d("DISHES", e.toString());
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        model.setPhoto(bmp);
    }
}

It works if I do both data/image download proccess in one AsyncTask doInBackground(String... params), but it doesnt when I split data and image downloading into seperate async tasks. Furthermore I dont get any exceptions or errors.

UPDATE: Images shows up when i switch views..

Rokas
  • 1,712
  • 2
  • 18
  • 35

1 Answers1

0

At first, getImage and getData are classes, and classes names in Java are capitalized.

Technically, you can run another AsyncTask from onProgressUpdate() or onPostExecute() - https://stackoverflow.com/a/5780190/1159507 So, try to put the breakpoint in second AsyncTask call and debug is it called.

Community
  • 1
  • 1
anber
  • 3,463
  • 6
  • 39
  • 68
  • I do call GetImage in onPostExecute and it works. I think something is wrong with model passing and setting image in another async task.. but I am not sure. – Rokas Feb 13 '14 at 14:34
  • If it works in one AsyncTask you can compare in debuger whats go wrong if you use two. – anber Feb 13 '14 at 17:12
  • Just noticed, that images appears if I switch to another tab and switch back - images appears.. – Rokas Feb 13 '14 at 17:34