0

我想将进度对话框与 asynctask 一起使用。我试过了,但它在收到响应后向我显示了对话框。这是我的调用函数..到 asycnk 任务正如我所料,我面临的问题是因为这个AppResponse = reqClient.execute().get(); 因为我也必须从这个异步任务中获取返回值。所以请参阅这两个文件以供参考

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

所以请建议我如何显示进程对话框。我尝试了stackoverflow上的所有示例。但是所有这些都不适合我。请帮助

4

5 回答 5

1

使用这种方式这对我来说很完美

您可以创建接口,将其传递给AsyncTask(在构造函数中),然后调用方法onPostExecute

例如:

你的界面:

public interface OnTaskCompleted{
    void onTaskCompleted();
}

您的活动:

public YourActivity implements OnTaskCompleted{
    //your Activity
}

还有你的 AsyncTask:

public YourTask extends AsyncTask<Object,Object,Object>{ //change Object to required type
    private OnTaskCompleted listener;

    public YourTask(OnTaskCompleted listener){
        this.listener=listener;
    }

    //required methods

    protected void onPostExecute(Object o){
        //your stuff
        listener.onTaskCompleted();
    }
}

确保永远不会调用AppResponse = reqClient.execute().get();它会阻塞你的 uithread

于 2013-07-23T07:32:31.627 回答
1

你可以试试这个

extends AsyncTask<String, Void, String>{
Context context;

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

@Override
protected void onPreExecute() {
    super.onPreExecute();
    progressDialog = new ProgressDialog(context);
    progressDialog.setMessage("Loading. Please Wait");
    progressDialog.setIndeterminate(false);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setCancelable(true);
    progressDialog.show();
}

@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); 
     progressDialog.dismiss();
        }
}
于 2013-07-23T07:32:54.257 回答
1

只是基于以前的答案,您可以通过修改侦听器函数以获取作为返回数据的参数来获取从异步任务返回的数据

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

然后在异步任务的 postExecute 函数中,让它调用带有返回数据的侦听器

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

然后在你调用异步任务的地方你只需要实现监听器。您可以让一个类实现侦听器或使用匿名类

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 任务的回调后,您可以将其关闭。

//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();
于 2013-07-23T11:34:26.367 回答
0

可以看看这段代码。这可以是简单的例子

public class LoginSync extends
            AsyncTask<ArrayList<NameValuePair>, Void, UserVO> {

        @Override
        protected UserVO doInBackground(ArrayList<NameValuePair>... params) {
            // TODO Auto-generated method stub
            return WebServiceClient.SendHttpPost("/us/si",
                    params[0], Use.class);
        }

        @Override
        protected void onPostExecute(UserVO result) {
            // TODO Auto-generated method stub
            if (result == null) {
                progressLayout.setVisibility(View.GONE);
                Toast.makeText(getApplicationContext(),
                        "Bağlantı Sorunu Tekrar Deneyiniz. ", 500).show();
                return;
            }
            UserVO userVO = new UserVO();
            userVO.setUserName(result.getUserName());
            userName = result.getUserIdentificationVO().getFirstName();
            userSName = result.getUserIdentificationVO().getLastName();



            progressLayout.setVisibility(View.GONE);

        }

    }
于 2013-07-23T07:33:56.047 回答
0

为此,您可以在 asyncTask 中使用 onProgressUpdate() 。每次您从 doInBackground() 执行 showProgressUpdate() 时都会调用它

于 2013-07-23T07:24:40.850 回答