Problems with Asynctask

Asked

Viewed 74 times

1

I’m trying to file a request for JSON, along with Asynctask. With this, I called the JSON in the InBackground method, however, the code executes the Jsonarrayrequest onResponse method after the onPostExecute call, and not at the time of its proper call.

public class MainActivity extends AppCompatActivity {

    private TextView tvCarregandoLogin;
    private GifTextView gifLogin;

    private String resposta = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lay_login);

        tvCarregandoLogin = (TextView)findViewById(R.id.tvCarregandoLogin);
        gifLogin = (GifTextView)findViewById(R.id.gifLogin);
        tvCarregandoLogin.setVisibility(View.INVISIBLE);
        gifLogin.setVisibility(View.INVISIBLE);

        new GetTask().execute();
    }

    class GetTask extends AsyncTask<Object, Void, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            gifLogin.setVisibility(View.VISIBLE);
            tvCarregandoLogin.setVisibility(View.VISIBLE);
        }

        @Override
        protected String doInBackground(Object... params) {

            JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, Auxiliar.json_url_getEspecialidades, (String)null, new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    String descricao = "";

                    for(int i=0; i<response.length(); i++)
                    {
                        try {
                            JSONObject jsonObject = response.getJSONObject(i);

                            descricao = jsonObject.getString("Especialidade");
                            resposta += descricao + "~";

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    onEspecialidadeIsReady(resposta);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                }
            });
            MySingleton.getInstance(MainActivity.this).addToRequest(jsonArrayRequest);

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            gifLogin.setVisibility(View.INVISIBLE);
            tvCarregandoLogin.setVisibility(View.INVISIBLE);

            //fazer alguma coisa com a variavel resposta
        }
    }

    public void onEspecialidadeIsReady(String r)
    {
        resposta = r;
    }
}

Mysingleton class

public class MySingleton {

    private static MySingleton mInstance;
    private RequestQueue requestQueue;
    private static Context mCtx;

    private MySingleton(Context context)
    {
        mCtx = context;
        requestQueue = getRequest();
    }

    public RequestQueue getRequest()
    {
        if(requestQueue == null)
        {
            requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return requestQueue;
    }

    public static synchronized MySingleton getInstance(Context context)
    {
        if(mInstance == null)
        {
            mInstance = new MySingleton(context);
        }
        return mInstance;
    }

    public<T> void addToRequest(Request<T> request)
    {
        requestQueue.add(request);
    }
}
  • Where is the error?

  • It enters the onResponse method after onPostExecute. onPostExecute should not be the last thing to run?

  • 2

    Why are you using an Asynctask? Jsonarrayrequest is already asynchronous.

  • Most likely due to lack of knowledge. In fact, what I need is just to display something while I do the JSON request;

1 answer

4


The use of Asynctask, in this case, makes no sense, Jsonarrayrequest is already asynchronous.

The method onPostExecute() is to be called before the onResponse() because the task performed on onExecute() is faster than that performed by Jsonarrayrequest.

onExecute() just add the request at the Queue of Volley, returning at once.

Change the code to only use the Jsonarrayrequest.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.