views:

108

answers:

2

Hello,

I am fetching an XML data from the web using HTTP GET/POST. Right now i have done it in simple way (i.e. without threaed).

According to the below link, i tried to implement it with Progress bar dialog with Thread http://www.ceveni.com/2009/09/sample-progress-bar-dialog-in-android.html

But how do i come to know about the finish time of fetching XML from the web.(i.e. what should be the sleep time and also when to dismiss the progress bar dialog box)

Let me clear more about my problem => In activity,when the user click on "Fetch data" button, the "Progress bar" dialog box should be appeared and it should be disappear only when the fetching is completed successfully.

I think this can be done with "AsyncTask" but dont know how to use that concept for this problem.

So how do i do it ?

+3  A: 
    public  void onClick() {
        new FetchTask(context).execute(null);
    }

    public class FetchTask extends AsyncTask<Object, Object, Object > {

    private ProgressDialog dlg;
    private Context ctx;

    public FetchTask(Context context) {
        ctx = context;
    }

    @Override
    protected void onPreExecute() {
        dlg = new ProgressDialog(ctx);
        dlg.setMessage("Loading....");
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Object result) {
        dlg.dismiss();
        if ( result instanceof Exception ) {
            // show error message
        } else {
            // display data
        }
        super.onPostExecute(result);
    }

    @Override
    protected Object doInBackground(Object... params) {

        try {
        // String rawData = doPost("yourUrl");
        // XMLTree data = yourParser.parse(rawData);
        // return data;
        } catch ( Exception e ) {
            return e;
        }

    }

}
Damian Kołakowski
@Damian got the idea ,but where you have written about showing dialog as soon as the button clicked ?
PM - Paresh Mayani
yourBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { new FetchTask(context).execute(null); } } );
Damian Kołakowski
@Damian ya it is there, but i am not getting anywhere "dialog.show()" or "showdialog()" method
PM - Paresh Mayani
The showDialog should be in `onPreExecute()` and the dismiss should be in the `onPostExecute()`
WarrenFaith
+1  A: 

You are right, ASyncTask is what you are looking for. Some points to start:

WarrenFaith
@WarrenFaith just now, i passed through all these links, however thanx for the support
PM - Paresh Mayani