I'm new to volley and i need to post an array of custom object to PHP api in order to save it in MYSQL database how can i accomplish this ?
Note : I know that my below code will pass only the last object of my array only
Here is my object :
public class object {
String name;
Integer qty;
Integer price;
}
and my code :
final ArrayList<object> myarray = new ArrayList<>();
myarray.add(ob1);
myarray.add(ob2);
RequestQueue queue = Volley.newRequestQueue(this);
JSONObject jsonArray = new JSONObject();
try {
int i = 0;
for (object obj : myarray) {
jsonArray.put("id",i++);
jsonArray.put("name",obj.name);
jsonArray.put("qty",obj.qty);
jsonArray.put("price",obj.price);
}
Log.i("JSON ARRAY ", jsonArray.toString());
} catch (Exception e) {
e.printStackTrace();
}
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, API_URL, jsonArray, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("RESPONSE",response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<>();
return params;
}
}; queue.add(request);
}