I am trying to send parameters from Android to PHP for my project.
Here's how I send the parameters. Please note that I haven't included the json array on the list of CartRequest
yet since I am not sure on how to do it. Please also note that I am not sure if my GSON conversion of ArrayList to Jason Array is working. The reason I wanted to know how to send it to PHP is so that I can check if GSON conversion is actually working
public void sendData(){
String total_amount = cartTotal.getText().toString();
String user_id = "28";
calender = Calendar.getInstance();
dateformat = new SimpleDateFormat("yyyy-MM-dd");
timeformat = new SimpleDateFormat("HH:mm:ss");
date = dateformat.format(calender.getTime());
time = timeformat.format(calender.getTime());
listProducts = ShoppingCartHelper.getCartList();
/** Gson gson = new GsonBuilder().create();
JsonArray cartitems = gson.toJsonTree(listProducts).getAsJsonArray(); **/
/** UPDATE: Changed GSON to for loop**/
JSONObject obj = new JSONObject();
JSONArray cartitems = new JSONArray();
for (int i=0; i < listProducts.size(); i++) {
try {
obj.put("id", id);
obj.put("quantity",quantity);
cartitems.put(obj);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
CartRequest cartRequest = new CartRequest(total_amount, user_id, date, time);
RequestQueue queue = Volley.newRequestQueue(ShoppingCartActivity.this);
queue.add(cartRequest);
}
I want to include the Json array cartitems
on my cartRequest
and queue it. I will only need the id
and quantity
from the Json array cartitems
Here's my CartRequest code:
public class CartRequest extends StringRequest {
private static final String REQUEST_URL = "MY_URL";
private Map<String, String> params;
public CartRequest(String total_amount, String user_id, String date, String time){
super(Request.Method.POST, REQUEST_URL, null, null);
params = new HashMap<>();
params.put("total_amount", total_amount);
params.put("user_id", user_id);
params.put("date", date);
params.put("time", time);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
I will appreciate any help. Thanks in advance!
UPDATE: getting an error after I added the loop.
wrong 2nd argument type. Found: 'org.json.JSONARRAY', required: 'java.lang.String'
you have to make cartItems JSONArray JSONArray cartItems = new JSONArray(); run a loop to add items from your listProducts into cartItems with id and quantity as jsonobjects for each array item.
public CartRequest(String total_amount, String user_id, String date, String time, JSONArray cartItems){
super(Request.Method.POST, REQUEST_URL, null, null);
params = new HashMap<>();
params.put("total_amount", total_amount);
params.put("user_id", user_id);
params.put("date", date);
params.put("time", time);
params.put("cartitems", cartItems);
}
//JsonArray request
void jsonArrayRequest(){
String url = "your_url_here";
RequestQueue queue = Volley.newRequestQueue(this);
JsonArrayRequest req = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
});
queue.addRequest(req);
}
refer http://www.androidhive.info/2014/09/android-json-parsing-using-volley/ http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.