-1

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.

ncr07
  • 57
  • 1
  • 7

2 Answers2

1

What I must do to avoid this exception

Get rid of runOnUiThread().

Then, do the JSON parsing in doInBackground() and move the UI-updating logic (e.g., setText() calls) to onPostExecute().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

You cant not perform network operations on the UI thread, and then you explicitly ask the UI thread to do them

Delete runOnuiThread and leave it on DoInBackground.

Put this on postexecute:

                    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));
Ringo
  • 850
  • 9
  • 24
  • To do this on postexecute i have to declare the variable JSONObject group as a global variable. Should I change the parameter type of onPostExecute ? – ncr07 Aug 01 '14 at 06:23
  • it is not mandatory, you can create an POJO, or send a string array to the onpostexecute, do not send the json object, send something that is meaningful to the UI, keep your abstraction levels in different places. you cando something like public class Group{ private String name; private String property1; private String property2; } set the getters and setters and build this object in your doinbackground, and send it to the on post execute. – Ringo Aug 01 '14 at 18:02