Java – create volley requests in different threads

I want to use the volley Library in different threads for requests

This means that there is a connection in the data process in the tree and uithread I want to do this because I have a lot of connections, so a lot of data needs to be processed. Today I have a user interface that is a block

So how can I place and start connections in different threads and execute onresponse() / onerrorresponse() in 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);

        }
    });

Solution

Every network request executed by volley is executed in the background thread Volley is taking care of this behind the scenes So there is no need to execute a request on a different thread because it is already happening

Listeners, on the other hand, are called on the UI thread

When you write data and process it on the UI thread, you basically answer your own questions Just move the data processing performed in the listener to the background thread / asynctask to free up your UI thread and prevent blocking

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>