如何使用进度对话框与异步任务 [英] How to use Progress Dialog with Async Task

查看:225
本文介绍了如何使用进度对话框与异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用进度与AsyncTask的对话框我如何使用that.I尝试,但它显示我的对话框得到响应后。这是我的呼唤function..to的asycnk任务
正如我期待我面临的问题是,因为这个鸭presponse的= reqClient.execute()获得();
因为我必须从这个异步任务获得的返回值。所以也请看到这两个做参考文件

I want to use progress dialog with asynctask how could I use that.I tried but it showing me the dialog after getting the response .this is my calling function..to the asycnk task As I am expecting the issue I am facing is because of this AppResponse = reqClient.execute().get(); Because I have to get the return value also from this Async task .So please see both the files for refrence

 void postHttpRequest(String userId,String pass,TextView error){
            RequestClient reqClient = new RequestClient(IweenTravelLoginPage.this);
            String AppResponse = null;
            try {
                url = "";
                Log.d("Http Post URL is ", url);
                AppResponse = reqClient.execute().get();

                String status = ValidateLoginStatus.checkLoginStatus(AppResponse);
                Log.d("Status recived", status);

                if(status.equals("200")){
                    saveInformation(userId,pass);
                    startingActivity();
                }else{
                    error.setText("Incorrect UserName or Password");
                }
            } catch (Exception e) {
                Log.e("Exception Occured", "Exception is "+e.getMessage());
            }

这功能是发送请求

public class RequestClient extends AsyncTask<String, Void, String>{
    Context context;

    public RequestClient(Context c) {
        context = c;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... aurl){
    String responseString="";
    HttpClient client = null;
    try {
         client = new DefaultHttpClient();  
         HttpGet get = new HttpGet(IweenTravelLoginPage.url);
         HttpResponse responseGet = client.execute(get);  
         HttpEntity resEntityGet = responseGet.getEntity();  
         if (resEntityGet != null) {  
             responseString = EntityUtils.toString(resEntityGet);
             Log.i("GET RESPONSE", responseString.trim());
         }
    } catch (Exception e) {
        Log.d("ANDRO_ASYNC_ERROR", "Error is "+e.toString());
    }
        Log.d("ANDRO_ASYNC_RESPONSE", responseString.trim());
        client.getConnectionManager().shutdown();
     return responseString.trim();

    }


    @Override
    protected void onPostExecute(String response) {
         super.onPostExecute(response); 
        }
}

所以,请建议我什么,我必须做的展现过程dialog.I试图在计算器。但所有的例子都没有工作me.Please帮助

So please suggest me what I have to do to show the process dialog.I tried all the example on the stackoverflow .But that all are not working for me.Please help

推荐答案

只是建立在previous答案,你可以把数据从异步任务通过修改侦听器函数取一个参数,它是返回返回数据

just building on previous answers, you can get the data returned from the Async task by modifying the listener function to take in a parameter which is the return data

public interface OnTaskCompleted{
    void onTaskCompleted(String response);
}

然后在异步任务的postExecute功能有它调用的返回数据听者

Then in the postExecute function of the Async task have it call the listener with the return data

    @Override
    protected void onPostExecute(String response){
        //your stuff
        listener.onTaskCompleted(response);
    }

又在哪里调用异步任务,你只需要实现监听。你可以有一个类实现监听器或使用匿名类

Then where you call the Async task you just need to implement the listener. you can either have a class implement the listener or use an anonymous class

OnTaskCompleted listener = new OnTaskCompleted() {
    void onTaskCompleted(String response){
        // We got return data from the Async Task, do stuff!
    }
};
RequestClient reqClient = new RequestClient(listener);
reqClient.execute();

为了显示进度对话框,你应该将它设置为显示你叫异步任务之前,然后,一旦你已经接收到呼叫从Aysnc任务回来,你可以辞退了。

In order to show a progress dialog you should set it to display before you call the async task, and then once you have received the call back from the Aysnc task you can dismiss it.

//make progressDialog a member of your class
progressDialog = new ProgressDialog(IweenTravelLoginPage.this);
progressDialog.setMessage("Loading");
progressDialog.show();

OnTaskCompleted listener = new OnTaskCompleted() {
    void onTaskCompleted(String response){
        // get rid of the progress dialog
        progressDialog.dismiss;
        // We got return data from the Async Task, do stuff!
    }
};
RequestClient reqClient = new RequestClient(listener);
reqClient.execute();

这篇关于如何使用进度对话框与异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆