How to add AsyncTask to this task?

0

Hello, could anyone help me add an AsyncTask to this task? Here I am connecting to a JSON for information, then it will be added to RecyclerView.

RequestQueue queue = Volley.newRequestQueue(this);
        String shhik = "http://meusite/arquivo.json";
        StringRequest stringRequest = new StringRequest(Request.Method.GET, shhik, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Response " + response);
                GsonBuilder builder = new GsonBuilder();
                Gson mGson = builder.create();
                List<receive_info> posts = new ArrayList<receive_info>();
                posts = Arrays.asList(mGson.fromJson(response, receive_info[].class));
                adapter = new adapter_info(Main.this, posts);
                recyclerView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "Erro ao tentar conectar com servidor!", Toast.LENGTH_SHORT).show();
                Log.d(TAG, "Error " + error.getMessage());
            }
        });
        queue.add(stringRequest);
    }

Thank you!

    
asked by anonymous 05.10.2016 / 21:36

1 answer

3

You do not need to add an AsyncTask to be able to show a ProgressBar . Volley processes the request asynchronously.

In this perspective and by comparison to AsyncTask , the onResponse() method, or the onErrorResponse() if there is an error, corresponds to the onPostExecute() method. What to put before queue.add(stringRequest); "matches" to be placed in onPreExecute() .

So you should start the ProgressBar on the line before queue.add(stringRequest); and end it in the methods onResponse() and onErrorResponse() .

    
07.10.2016 / 11:01