I am trying to get data from a webserver and display a message saying "ok" or "invalid key". While it is getting the data it should produce a progress dialog. I have tried many methods including putting it inside of a thread however inside a thread the alert dialogs won't work. I have tried to use the async task method but it doesnt seem to work either. Can someone help me find a solution? Thanks
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();
Str开发者_运维百科ing 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;
}
}
You can't show any kind of dialog or progress dialog from the doInBackground()
method because it doesn't run on the UI Thread. You will want to update the UI with progress updates using the onProgressUpdate()
method. If you want to show an AlertDialog
on completion, use the onPostExecute()
method. Both onProgressUpdate()
and onPostExecute()
run on the UI thread, so your AlertDialog
code will work properly.
Here's the AsyncTask
page with some sample code on how to properly use it (including where to provide updates to the UI): http://developer.android.com/reference/android/os/AsyncTask.html
Here's some SO questions covering the same topic: 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
Out of curiosity...what happens if you remove the "dismiss" call immediatelty underneath your line where you show the dialogue? Any chance the dialogue is getting dismissed immediately upon creation?
Also...with other dialogues you have to go
dialogue.show()
Is a progress dialogue any different? From the docs....
Opening a progress dialog can be as simple as calling ProgressDialog.show(). For example, the progress dialog shown to the right can be easily achieved without managing the dialog through the onCreateDialog(int) callback, as shown here:
精彩评论