你好@All out there :) 我有一个小问题。当我单击登录时,我想显示一个 ProgressDialog,但没有显示任何内容,它只是在没有 ProgressDialog 的情况下执行任务。我正在使用 AsyncTask 并在该线程中打开 ProgressDialog 但没有出现。这是我的源代码:
它是这样调用的:
ProgressDialog progress = new ProgressDialog(mainActivity);
progress.setMessage("Sie werden registriert...");
jsonParser = new JSONParser(progress, url_create_user, "POST", params);
这是 JSONParser 类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
public class JSONParser extends AsyncTask<Context, Void, JSONObject>{
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
String url = "";
String method = "";
List<NameValuePair> parameters;
ProgressDialog prog;
// constructor
public JSONParser(ProgressDialog prog, String url, String method, List<NameValuePair> param) {
this.url = url;
this.method = method;
this.parameters = param;
this.prog = prog;
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0)
{
prog.show();
}
else
{
prog.dismiss();
}
}
};
@Override
protected JSONObject doInBackground(Context... params) {
try {
handler.sendEmptyMessage(0);
// Überprüfen welche Request Methode benutzt werden soll
if(method == "POST"){
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
CookiePolicy.BROWSER_COMPATIBILITY);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(this.parameters));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(this.parameters, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//Stream in ein String umwandeln
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Fehler!", "Fehler mein umwandeln von Stream in String: " + e.toString());
}
// JSON Object parsen
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error beim parsen " + e.toString());
}
handler.sendEmptyMessage(1);
// Das JSONObject zurückgeben
return jObj;
}
}