ProgressDialog wird in AsyncTask nicht angezeigt

Ich erstelle eine Android-App, die von den Daten abhängt, die die App aus der Datenbank erhält. Um diese Daten zu erhalten, habe ich die folgende Klasse (diese Klasse holt Daten aus der Datenbank in json, übersetzt sie und gibt sie zurück):

public class Json {

public String jsonResult;

private Activity activity;
private String url = "http://json.example.org/json.php";
private String db, query;

public Json(Activity activity) {
    this.activity = activity;
}

public String accessWebService(String db, String query) {
    JsonReadTask task = new JsonReadTask();

    this.db = db;
    this.query = query;

    task.execute(new String[] { url });

    try {
        task.get();
    } catch (InterruptedException e) {
        Toast.makeText(activity.getApplicationContext(),
                "FATAL ERROR: The thread got interrupted", Toast.LENGTH_LONG).show();
    } catch (ExecutionException e) {
        Toast.makeText(activity.getApplicationContext(),
                "FATAL ERROR: The thread wasn't able to execute", Toast.LENGTH_LONG).show();
    }
    return jsonResult;
}

// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {

    private final ProgressDialog dialog = new ProgressDialog(activity);

    protected String doInBackground(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(params[0]);
        try {
            // add post data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("db", db));
            nameValuePairs.add(new BasicNameValuePair("query", query));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
            if (jsonResult.isEmpty()) {
                Toast.makeText(activity.getApplicationContext(),
                        "Error, connection is up but didn't receive data. That's strange...",
                        Toast.LENGTH_LONG).show();
                this.cancel(true);
            }

        } catch (ClientProtocolException e) {
            //Toast.makeText(activity.getApplicationContext(),
            //      "Error, Client Protocol Exception in JSON task",
            //      Toast.LENGTH_LONG).show();
            Log.i("Json", "Error, Client Protocol Exception in JSON task");
            this.cancel(true);
        } catch (IOException e) {
            //Toast.makeText(activity.getApplicationContext(),
            //      "Error, Please check your internet connection",
            //      Toast.LENGTH_LONG).show();
            Log.i("Json", "Error, Please check your internet connection");
            this.cancel(true);
        }
        return null;
    }

    private StringBuilder inputStreamToString(InputStream is) {
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        try {
            while ((rLine = rd.readLine()) != null) {
                answer.append(rLine);
            }
        } catch (IOException e) {
            Toast.makeText(activity.getApplicationContext(), "Error..." + e.toString(),
                    Toast.LENGTH_LONG).show();
        }
        return answer;
    }


    }
}// end async task

}

Ich habe festgestellt, dass meine App beim Zugriff auf die Datenbank abstürzt. Nach einigem googeln habe ich herausgefunden, dass die .get () Methode in der accessWebService () Methode dies verursacht hat. Ich habe versucht, einen progressDialog wie folgt zu implementieren (ich habe auch die .get () -Methode gelöscht):

private final ProgressDialog dialog = new ProgressDialog(activity);

    protected void onPreExecute() {
        super.onPreExecute();
        this.dialog.setMessage("Loading...");
        this.dialog.setCancelable(false);
        this.dialog.show();
    }

protected void onPostExecute(String result) {
            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
            }
        }

Aber der Dialog wurde nicht angezeigt und ich habe NullPointerExceptions erhalten, da die App nur funktioniert, wenn Daten vorhanden sind:

result = json.accessWebService(db, query);

(Vielleicht eine wichtige Sache zu erwähnen: Ich benutze diese Methode auch in Forloops)

Meine Frage lautet nun: Wie kann ich meine App so ändern, dass ich beim Zugriff auf die Datenbank einen ProgressDialog erhalte, ohne NullPointerExceptions zu erhalten? Ich befürchte, dass ich meine gesamte App neu entwickeln muss und wenn ich das tun muss, wie mache ich das? Ich hoffe ihr versteht meine Frage und habt eine Lösung dafür, weil ich wirklich Hilfe brauche. Danke im Voraus.

P.S. Sorry, wenn mein Englisch nicht so gut ist, ich bin kein Muttersprachler.

Antworten auf die Frage(3)

Ihre Antwort auf die Frage