android - ProcessDialog 没有正确显示?

标签 android

这是我在 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());
        }
    }

从这个函数我调用了一个用于 Http 通信的 AsynkTask。所以当我收到响应时单击按钮然后我的 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 正确显示在设备的中央

最佳答案

//在你的progressbialog中添加样式

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

关于android - ProcessDialog 没有正确显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17398837/

相关文章:

java - 从 JAXB 迁移到 Castor 的简单方法?

java - 无法解析 : com. android.support.test.espresso :espresso-intents:27. 0.2

java - 测试仍然从真实的SharedPreferences中读取

Android 视频录制 - mediaRecorder.start 上的 IllegalStateException

android - 某些 androids 应用程序无法通过 fiddler 连接

android - 激活电池优化时如何执行后台服务

android如何设置以编程方式准备的arrayAdapter布局

android - 方法解析中的歧义以及如何解决它

java - 使用线程将图像加载到 recyclerview

Android View 设置文字颜色