Question

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 ?

Was it helpful?

Solution

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

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top