我正在尝试从网络服务器获取数据并显示一条消息“ok”或“invalid key”。在获取数据时,它应该生成一个进度对话框。我已经尝试了很多方法,包括将它放在一个线程中但是在一个线程中,警告对话框将不起作用。我试图使用异步任务方法但它似乎也没有工作。有人可以帮我找到解决方案吗?感谢
verifyCode.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//I surround the async task with the progressdialog so that it should show while the method is working in the background
final ProgressDialog progressDialog = ProgressDialog.show(
Activate.this, "", "Loading...");
new checkActivationCode().execute(activationCode.toString().toUpperCase()
);
progressDialog.dismiss();
}
});
public class checkActivationCode extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... activationCode) {
// TODO Auto-generated method stub
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(
"https://iphone-radar.com/accounts/confirmation");
JSONObject holder = new JSONObject();
holder.put("code", activationCode);
StringEntity se = new StringEntity(holder.toString());
httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
ResponseHandler responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httpost, responseHandler);
if (response != null) {
org.json.JSONObject obj = new org.json.JSONObject(response);
if ("00000000-0000-0000-0000-000000000000".equals(obj
.getString("id"))) {
new AlertDialog.Builder(Activate.this)
.setTitle(
getResources().getString(
R.string.InvalidKey))
.setMessage(
getResources()
.getString(
R.string.PleaseEntervalidRegistration))
.setNeutralButton("OK", null).show();
} else {
// add permanent variables
SharedPreferences prefs = getSharedPreferences(
"Settings", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("ACTIVATED", true);
editor.putString("ID", obj.getString("id"));
editor.commit();
Intent imTracking = new Intent(Activate.this,
ImTracking.class);
imTracking.putExtra("ActivationSuccessful", true);
// transfer more data
startActivity(imTracking);
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
答案 0 :(得分:3)
您无法从doInBackground()
方法显示任何类型的对话框或进度对话框,因为它不会在UI线程上运行。您需要使用onProgressUpdate()
方法使用进度更新来更新UI。如果您想在完成时显示AlertDialog
,请使用onPostExecute()
方法。 {0}}和onProgressUpdate()
都在UI线程上运行,因此您的onPostExecute()
代码可以正常运行。
以下是AlertDialog
页面,其中包含有关如何正确使用它的示例代码(包括在哪里提供对UI的更新):http://developer.android.com/reference/android/os/AsyncTask.html
以下是一些涉及相同主题的SO问题:How to use AsyncTask to show a ProgressDialog while doing background work in Android?
Updating progress dialog in Activity from AsyncTask
How can I remove an alert dialog after a AsyncTask has done it's work
答案 1 :(得分:0)
出于好奇......如果你在显示对话线的线下面删除了“解雇”电话,会发生什么?对话在创作后是否有机会立即被解雇?
另外......与其他对话你必须去
dialogue.show()
进步对话有什么不同吗?来自文档......
打开进度对话框可以像调用ProgressDialog.show()一样简单。例如,如果没有通过onCreateDialog(int)回调管理对话框,可以轻松实现右侧显示的进度对话框,如下所示: