0

Toast is showed around of 2 times, and Json is returning fine 6 objects.

Idk if its problem of RunOnUiThread (I know that is not good for use in asyntask) or otherthing.

what other way can I use for my variable "mensaje" every time that for put a string in that I can show a Toat, maybe in OnPostExecute.

class asyncMensaje extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            String user = params[0];
            ArrayList<NameValuePair> envioDato = new ArrayList<NameValuePair>();
            envioDato.add(new BasicNameValuePair("rut", user));
            JSONArray jdata2 = post.getserverdata(envioDato, URL_connectFechas);

                if (jdata2 != null && jdata2.length() > 0) {
                JSONObject json_data; // creamos un objeto JSON
                for (int i = 0; i < jdata2.length(); i++) { 
                    try {   
                    json_data = jdata2.getJSONObject(i);
                    mensaje = json_data.getString("mensaje");
                    Log.e("Info: ", "" + mensaje);
                    } catch (JSONException e) {

                        e.printStackTrace();
            }
                 runOnUiThread(new Runnable() {

                        public void run() {

                          Toast.makeText(getApplicationContext(), mensaje, Toast.LENGTH_LONG).show();

                           }
                        });


              }
            }


            return null;
        }

        protected void onPostExecute(String result) {



        }

    }

2 Answers2

1

The ideal solution in your case is to use onProgressUpdate together with sequencing of Toasts. Refer to this answer for doing the latter.

class asyncMensaje extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            //....
            for (int i = 0; i < jdata2.length(); i++) {
              //..
              publishProgress(mensaje);
            }
        }
        @Override
        protected void onProgressUpdate(String... progress) {
            //code for showing Toast
        }
}
Community
  • 1
  • 1
Rajesh
  • 15,724
  • 7
  • 46
  • 95
0

Use onPostExecute method for processing data after getting from webservice in doInBackground instead of using runOnUiThread in doInBackground which run on different Thread. do it as:

 @Override
        protected String doInBackground(String... params) {
            String user = params[0];
            ArrayList<NameValuePair> envioDato = new ArrayList<NameValuePair>();
            envioDato.add(new BasicNameValuePair("rut", user));
            JSONArray jdata2 = post.getserverdata(envioDato, URL_connectFechas);
            return jdata2.toString();
        }
   @Override
     protected void onPostExecute(String result) {
        JSONArray jdata2=new JSONArray(result);
         // parse JSONArray here and show Toast...

      }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213