3

I was just wondering which is the best way to fetch data from a web service using HTTP request. Using AsyncTask or AsyncTaskLoader

I'm using AsyncTaskLoader using LoaderManager on the activity but I encounter problems when there are configuration changes most especially orientation changes. It stops the loader from calling OnFinish().

I've also tried AsyncTask which is more flexible and independent, works well on my previous project which was still on 2.2? Since AsyncTaskLoader is more recommended in 3.0---4.0 I'd appreciate your opinions.

  • Can you update your data from your custom adapter when you use asynctaskloader? – Mohit Verma Sep 23 '13 at 11:22
  • @Mohit:Yes, try to implement it in 'onLoadFinish()' and if it has an exception that says "only the activity can touch it's child views etc.." you can run a thread to update the views using a Handler – CarlCarrots Oct 14 '13 at 01:36

1 Answers1

1

For AsyncTask write below from where you want to start task
new asnktask(youractivity.this).execute(); And write this class

private class asnktask extends AsyncTask<Void, Void, Integer> {


    private Context context;    
    private ProgressDialog mProgressDialog;

    public asnktask(Context _context) {
        context =_context;
    }

    @Override
    protected void onPreExecute() {
        mProgressDialog = new ProgressDialog(this.context);
        mProgressDialog.setMessage("Loading wait.....");
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.setCancelable(true);
        mProgressDialog.show();
    }

    @Override
    protected Integer doInBackground(Void... params) {
//Do stuff of getting web service response
        return 1;
    }

    @Override
    protected void onPostExecute(Integer result) {
        mProgressDialog.dismiss();
}
    }

//Done!!!!!!!!

codercat
  • 22,873
  • 9
  • 61
  • 85
Dhrupal
  • 1,863
  • 1
  • 23
  • 38