开发者

Android - Fetching XML Data from web(Using AsyncTask) and Progressbar related problem

开发者 https://www.devze.com 2023-01-16 00:34 出处:网络
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).

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).

Ac开发者_JAVA百科cording 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 ?


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

    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消