when i execute this async task I get a networkOnmainThreadException:
class GetGroupBiography extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(BiographyActivity.this);
pDialog.setMessage("Loading biography details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting product details in background thread
* */
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("nombre_grupo", nombre_grupo));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(get_biography_url, "GET", params);
// check your log for json response
Log.d("Biography Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray groupObj = json
.getJSONArray(TAG_GROUP); // JSON Array
// get first product object from JSON Array
JSONObject group = groupObj.getJSONObject(0);
// product with this pid found
// Edit Text
textName = (EditText) findViewById(R.id.textName);
textAnyo = (EditText) findViewById(R.id.textDate);
textDescripcion = (EditText) findViewById(R.id.textDescription);
// display product data in EditText
textName.setText(group.getString(TAG_NAME));
textAnyo.setText(group.getString(TAG_ANYO));
textDescripcion.setText(group.getString(TAG_DESCRIPTION));
}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String result) {
// dismiss the dialog once got all details
super.onPostExecute(result);
if (pDialog.isShowing()) {
pDialog.dismiss();
// progressDialog.setCancelable(true);
}
}
}
I call this async task from this code section:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_biography);
AlertDialog.Builder ald = new AlertDialog.Builder(BiographyActivity.this);
ald.setTitle("Nombre del grupo");
ald.setMessage("Introduce el nombre del grupo:");
final EditText texto = new EditText(BiographyActivity.this);
ald.setView(texto);
ald.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
nombre_grupo = texto.getText().toString();
new GetGroupBiography().execute();
}
});
ald.show();
}
What I must do to avoid this exception. because i tried to move the json to onpostexecute() but it didn't work. I hope for a little help to perform this task because i must do it as soon as possible.