I have a problem wih my progressdialog, my app searches for the data from json and after that the dialog comes, but I want it at this time while he collects the data. where is my mistake?
There is the edited code logcat( FATAL EXCEPTION: main java.lang.NullPointerException) JSON ([{"name":"Test"}]) edit:
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new TheTask().execute();
}
class TheTask extends AsyncTask<Void, Void, JSONObject> {
InputStream is = null;
String result = "";
JSONObject jArray = null;
ProgressDialog pd;
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
pd.dismiss();
JSONObject jObject = result;
try {
String aJsonString = jObject.getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
setListAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, (List<String>) result));
// parse and set List adapter here
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(MainActivity.this, "dialog title",
"dialog message", true);
}
@Override
protected JSONObject doInBackground(Void... arg0) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("***");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
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();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}