调用Web服务时不显示“进度”对话框

时间:2012-06-29 05:34:11

标签: android

我是android新手。我想在用户点击登录按钮时显示进度对话框。我试过了,但对话框没有显示

btn_logIn.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View view) {
         getUserCredentials();
     }
}); //end of anonymous class

private void showProgressDialog() {     
    if (dialog == null) {       
        dialog = new ProgressDialog(this);          
    }
    dialog.setMessage("Please Wait. Your authentication is in progress");
    dialog.setButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which)              
            dialog.dismiss();               
        }           
    }); //end of anonymous class        
    dialog.show();  
} //end of showProgressDialog()

private void getUserCredentials() {

    EditText txt_userName = (EditText) findViewById(R.id.txt_userName);
    String userName = txt_userName.getText().toString();

    EditText txt_password = (EditText) findViewById(R.id.txt_password);
    String password = txt_password.getText().toString();

    if (userName != null && !userName.trim().equals("") && password != null && !password.trim().equals("")) {                   
        showProgressDialog();       
        callWebService(userName, password);         
    }
} //end of getUserCredentials()

private void callWebService(String userName, String password) {

    try {

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("userName", userName);
        ....            
        Object result = envelope.getResponse();             
        if (result.equals("true")) {                
            dialog.dismiss();
            Toast.makeText(this, result.toString(), Toast.LENGTH_SHORT).show();

        } else {                
            dialog.dismiss();
            Toast.makeText(this, result.toString(), Toast.LENGTH_SHORT).show();                             
        }

     } catch (SocketTimeoutException e) {            
         dialog.dismiss();
         Toast.makeText(this, "Service is not connected, Please make sure your server is running", Toast.LENGTH_LONG).show();                       
    }  catch(Exception e) {         
        e.printStackTrace();
        dialog.dismiss();
        Toast.makeText(this, "Unable to connect, please try again later. Thank you", Toast.LENGTH_LONG).show();                     
     }
} //end of callWebServide()

我做错了什么。当我点击登录按钮并且服务未运行时,它会显示服务未连接的消息,请确保您的服务器正在运行“,但对话框未显示...为什么?我的逻辑是当用户点击登录按钮并且字段有值然后开始显示进度对话框,如果发生任何事情,如结果来了或服务器没有运行或发生任何异常,那么我删除对话框并显示相应的消息,但对话框是显示......为什么?我做错了什么?请帮忙......

由于

2 个答案:

答案 0 :(得分:0)

当任务启动时使用AsyncTask初始化窗口小部件,然后从run方法调用web服务并关闭stop方法的进度条。

答案 1 :(得分:0)

试试这个,

像这样更改你的getUserCredentials(),

private void getUserCredentials() {

    EditText txt_userName = (EditText) findViewById(R.id.txt_userName);
    String userName = txt_userName.getText().toString();

    EditText txt_password = (EditText) findViewById(R.id.txt_password);
    String password = txt_password.getText().toString();

    if (userName != null && !userName.trim().equals("") && password != null && !password.trim().equals("")) {                   
        showProgressDialog();
 Thread t=new Thread(new Runnable() {

        public void run() {
             callWebService(userName, password);  

        }
    });   t.start();    
  }
}

你的 callWebService 方法,

private void callWebService(String userName, String password) {

try {

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("userName", userName);
    ....            
    Object result = envelope.getResponse();             
    if (result.equals("true")) {                
        dialog.dismiss();
        Toast.makeText(this, result.toString(), Toast.LENGTH_SHORT).show();

    } else {  
        ActivityName.this.runOnUiThread(new Runnable() {

    public void run() {
         dialog.dismiss();
        Toast.makeText(this, result.toString(), Toast.LENGTH_SHORT).show();  

    }
});              

    }

 } catch (SocketTimeoutException e) {  
      ActivityName.this.runOnUiThread(new Runnable() {

    public void run() {
          dialog.dismiss();
     Toast.makeText(this, "Service is not connected, Please make sure your server is running", Toast.LENGTH_LONG).show();                       
    }
});           

}  catch(Exception e) {         
    e.printStackTrace();
 ActivityName.this.runOnUiThread(new Runnable() {
    public void run() {
           dialog.dismiss();
    Toast.makeText(this, "Unable to connect, please try again later. Thank you", Toast.LENGTH_LONG).show();                       
    }
});   

 }

}

更新1

要从您的评论中回答您的问题,

1)是异步任务更有效。它有自己的方法来完成我在这里描述的相同任务。

AsyncTask有以下方法,

i)onPreExecute() - 可用于启动Dialog   ii)doInBackground() - 充当后台线程。   iii)onPostExecute() - 在你可以关闭对话框的末尾调用它。

我没有提到的原因是,您可能需要更改工作代码的结构以适应异步任务。

2) runonUiThread - 正如名称所示,其中的任何内容都将被视为在主UI线程中运行。所以基本上更新屏幕你必须使用它。还有其他可用的方法,如处理程序,也可以执行相同的任务。