17

I would like to make a request using the Android Volley library in a different thread.

What I mean is, there is the connection which is in the thread and the data is processed in the UI thread.

I want to do this because I have a lot of connections, a lot of data to process, and right now the user interface is blocked.

So, how do I create and start the connection in a different Thread, and do then OnResponse()/OnErrorResponse() in the UIThread?

JsonArrayRequest getReq = new JsonArrayRequest(url,new Response.Listener<JSONArray>() {

    @Override
    public void onResponse(JSONArray response) {
        Log.d("onRESPONSE Synchro -> Produit",response.toString());                                 
        PgrBarProducts.setMax(response.length());       
        percentDisplayProduct.setText("0/"+ PgrBarProducts.getMax());
        nbMaxCallNetwork = PgrBarProducts.getMax();
        try {
            for (int i = 0; i < response.length(); i++) {                                           
                JSONObject explrObject = response.getJSONObject(i);
                String id = Integer.toString((Integer) explrObject.get("id")); 

                callOneObject(id, PgrBarProducts, percentDisplayProduct , 1); // appel du product
             }
        } catch (JSONException e) {
            e.printStackTrace(new PrintWriter(stackTrace));
        }                           
    }
 }, new Response.ErrorListener() {
     @Override
     public void onErrorResponse(VolleyError error) {
         changeStatutToError();
         VolleyLog.d("", "Error: " + error.getMessage());
         percentDisplayProduct.setTextColor(Color.RED);
         percentDisplayProduct.setTypeface(null, Typeface.BOLD);
         percentDisplayProduct.setText("erreur");


         waitBarProgressProduct.setVisibility(View.INVISIBLE);
         synchroProducts.setVisibility(View.VISIBLE);
     }
});

getReq.setRetryPolicy(new DefaultRetryPolicy(60 * 1000, 
    1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));               
// Adding request to request queue and start the request        
AppController.getInstance().addToAndStartRequestQueue(getReq);

1

1 Answer 1

29

Every network request performed by Volley is performed in a background thread. Volley takes care of this behind the scenes. So there is no need to perform a request on a different thread, since that's already happening.

The listeners, on the other hand, are called on the UI thread.

You basically answered your own question when you wrote that the data is processed on the UI thread. Simply move that data processing that is performed inside your listeners to a background thread / AsyncTask to free your UI thread and prevent the blocking.

5
  • i resolve my problem by another method. I saw that the OnResponse work on the UI Thread and the request in the other thread like you said. But Using the parseNetworkResponse(NetworkResponse response) which work on other thread, i can do the process data in this method so in the other thread and just run the prograsseBar in the Onresponse. LIke this, the UiTHread is not block. After that , i don't know if it's a good method , what do you think about this ? Oct 30, 2014 at 8:37
  • I'm not sure I understood exactly, but it sounds like you moved your data processing to another thread like I suggested. If that's the case, then I'm happy it worked for you. Oct 30, 2014 at 10:39
  • @Itai so is using parseNetworkResponse + GSONRequest method of retrieving data solve the ui thread issue on the listener since it is not doing heavy computing on the onResponse? I implemented it but would like to clarify if my understanding is correct :)
    – Woppi
    Nov 30, 2016 at 10:33
  • @Woppi The network request, or data retrieval is performed in the background by Volley. The data processing, i.e. going over the data and manipulating it to your liking, is your responsibility - and as such should probably be performed in the background (unless it's trivial) Nov 30, 2016 at 11:00
  • Seems really inefficient that my app needs to switch the main thread to execute my response handler, when all my response handler does on the main thread is create a new thread. The handler should be on a background thread by default. Bad API design, Google. Sep 27, 2019 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.