我尝试在用户点击确认按钮后将POST方法发送到服务器,但点击后我得到了ProgressDialog仍在运行的响应。
paymentSubmitBtn = (Button)findViewById(R.id.paymentSubmitBtn);
paymentSubmitBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
final String receiver = receiverMymoIdEdtTxt.getText().toString();
runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(Payment.this);
builder.setTitle(R.string.message);
builder.setMessage("Transaction Details")
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
ProgressDialog dialog = ProgressDialog.show(Payment.this, "", "Connect to server..." , true);
sendTransactionInfo(receiver);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
});
这是我用来发送POST方法并尝试解除ProgressDialog的函数。
protected void sendTransactionInfo(final String receiver)
{
Thread t = new Thread() {
public void run() {
Looper.prepare(); //For Preparing Message Pool for the child Thread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
HttpResponse response;
String URL = baseURL + "/api-create-transaction";
try {
HttpPost post = new HttpPost(URL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("section", "API" ));
nameValuePairs.add(new BasicNameValuePair("receiver", receiver ));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = client.execute(post);
if(response!=null)
{
String res = EntityUtils.toString(response.getEntity());
final JSONObject result = new JSONObject(res);
String operation = result.getString("operation");
if(operation.trim().equalsIgnoreCase("success"))
{
JSONObject data = result.getJSONObject("data");
}
else if(operation.trim().equalsIgnoreCase("error"))
{
runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(Payment.this);
builder.setTitle(R.string.message);
try
{
builder.setMessage(result.getString("message"))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
AlertDialog alert = builder.create();
alert.show();
}
});
Toast.makeText(getBaseContext(), result.getString("message") , Toast.LENGTH_LONG).show();
}
dialog.dismiss();
}
} catch(final Exception e) {
runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(Payment.this);
builder.setTitle(R.string.message);
builder.setMessage(e.toString())
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
Looper.loop();
}
};
t.start();
}
答案 0 :(得分:0)
将对话框对象传递给sendTransactionInfo()或将ProgressDialog对话框设为全局将是我的建议。看起来它可能是一个范围问题(只是简单地查看您的代码)。
答案 1 :(得分:0)
嗯,试试这个改变
alert.dismiss();
但要使其成为全球性......对AlertDialog的引用