【问题标题】:ProcessDialog is not appearing properly?ProcessDialog 没有正确显示?
【发布时间】:2013-07-01 16:02:57
【问题描述】:

这是我在 LoginActivity.java 中的函数。所以点击按钮我正在调用这个函数。

public  void postHttpRequest(String userId,String pass,TextView error){
        RequestClient reqClient = new RequestClient(LoginActivity.this);
        String AppResponse = null;
        try {
            url = "myurl";
            Log.d("URL", url);
            AppResponse = reqClient.execute().get();
            String status = ValidateLoginStatus.checkLoginStatus(AppResponse);
            Log.d("Status recived", status);

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

从这个函数中,我正在调用一个 AsynkTask 进行 Http 通信。所以当我收到响应时单击按钮,然后我的 processDialog 只打开一秒钟。我希望当我单击按钮时我的 processDialog 应该打开直到我得到回应

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

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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Authenticating user...");
        pDialog.show();

    }

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

    }


    @Override
    protected void onPostExecute(String response) {
         super.onPostExecute(response); 
            if(pDialog!=null) 
            pDialog.dismiss();
    }
}

所以请建议我必须进行哪些更改才能使 processDialog 正确显示在设备的中心

【问题讨论】:

  • 也许您确实关闭了 onPostExecute 中的对话框。在里面放一个断点看看。
  • 你的进度现在根本没有显示出来吗?
  • 得到响应后,对话框仅打开 1 秒
  • @GauravPandey 你看到doInBackground中的日志消息了吗?
  • @GauravPandey 是您的异步​​任务,是您的活动类的内部类,还是单独的文件。

标签: android


【解决方案1】:

//在进度条中添加样式

 protected void onPreExecute() {
            super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog.setMessage("Authenticating user..."); 

if (pDialog != null && !pDialog.isShowing()) {
                        pDialog.show();
                    }
}

【讨论】:

    【解决方案2】:

    AsyncTask return value only after using get() method

    从上面的链接中绘图

    调用AsyncTaskget()方法会阻塞主线程,等待返回结果。这有效地使使用 AsyncTask 成为同步操作,在这种情况下使用 AsyncTask 毫无意义。

    我能想到使用 get() 方法的唯一原因是来自主 (UI) 线程以外的线程,尽管我想不出很多这样做的理由。

    点击按钮

           RequestClient reqClient = new RequestClient(LoginActivity.this,new TheInterface() {
                 @Override
                 public void theMethod(String result) {
                     Log.i("Result  =",result); 
                }  
            });
           reqClient.execute(url); // no get(). pass url to doInBackground()      
    

    在你的活动课上

        public interface TheInterface {  
        public void theMethod(String result);
    
           }
         }
    

    异步任务

    public class RequestClient extends AsyncTask<String, Void, String>{
        ProgressDialog pDialog;
        Context context;
        TheInterface listener; 
        public RequestClient(Context c,TheInterface listen) {
            context = c;
            listener = listen;   
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(context);
            pDialog.setMessage("Authenticating user...");
            pDialog.show();
    
        }
    
        @Override
        protected String doInBackground(String... aurl){
        String responseString="";
         HttpClient client; 
        try {
             HttpClient client = new DefaultHttpClient();  
             HttpGet get = new HttpGet(aurl[0]); // url 
             HttpResponse responseGet = client.execute(get);  
             HttpEntity resEntityGet = responseGet.getEntity();  
             if (resEntityGet != null) {  
                 responseString = EntityUtils.toString(resEntityGet);
                 Log.i("GET RESPONSE", responseString);
             }
        } catch (Exception e) {
            Log.d("ANDRO_ASYNC_ERROR", "Error is "+e.toString());
        }
        Log.d("ANDRO_ASYNC_ERROR", responseString);
         client.getConnectionManager().shutdown();
         return responseString;
    
        }
    
    
        @Override
        protected void onPostExecute(String response) {
             super.onPostExecute(response); 
                  pDialog.dismiss();
                  if (listener != null) 
                  {
                    listener.theMethod(result);
                  }
    
        }
    }
    

    【讨论】:

      【解决方案3】:

      您的按钮代码似乎不正确,因为它是异步的,但您正在尝试将其用作标准同步代码。

      尝试将此代码移动到 onPostExecute:

      String status = ValidateLoginStatus.checkLoginStatus(response);
                  Log.d("Status recived", status);
      
                  if(status.equals("200")){
                      saveInformation(userId,pass);
                      startingActivity(HOST_URL);
                  }else{
                      error.setText("Incorrect UserName or Password");
                  }
      

      并让这个按钮点击代码:

      public  void postHttpRequest(String userId,String pass,TextView error){
              RequestClient reqClient = new RequestClient(LoginActivity.this);
              String AppResponse = null;
              try {
                  url = "myurl";
                  Log.d("URL", url);
                  reqClient.execute();
              } catch (Exception e) {
                  Log.e("Exception Occured", "Exception is "+e.getMessage());
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2010-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多