Reputation: 11
Hello i'm trying to get a list of data with an php api with volley and foreach item i found i added to my Recipes ArrayList in a method called GetAll(), here's the code of the method :
requestQueue = Volley.newRequestQueue(context);
StringRequest stringRequest = new StringRequest(Request.Method.GET,
"http://192.168.137.1/supcookingapi/api.php?action=selectallrecipes",
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Recettes.clear();
String[] tableau = s.split("&nb");
for (String r : tableau) {
String[] data = r.split(";");
Recettes.add(new Recette (Integer.parseInt(data[0]), data[1], data[2], java.sql.Time.valueOf(data[3]), data[4],Integer.parseInt( data[5]),Integer.parseInt(data[6]),Integer.parseInt(data[7]),Integer.parseInt(data[8]),data[9]));
}
/*Toast.makeText(context, "Total users : " + usersList.size(), Toast.LENGTH_SHORT).show();*/
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(context, volleyError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(stringRequest);
Then i call this method in the MainActivity with Recipe.GetAll(), but i wanna kind of await to load all elements in the Recettes List then after i can for example Load my page
Upvotes: 0
Views: 504
Reputation: 746
Volley requests are already asynchronus, you don't need to make any extra async stuff. Just call your method in your Activity or Fragment and do whatever you wanna do with the Recettes arraylist after is loaded right after the for loop.
Upvotes: 1
Reputation: 3001
You can use the onPostExecute() method to call loadPage().
@Override
protected void onPostExecute(String result) {
//Do Your Work
}
onPostExecute() method is called when the AsyncTask completes its execution.
Upvotes: 0