繁体   English   中英

如何使用Volley RequestQueue将Json数组发送到PHP

[英]How to send Json array to PHP using Volley RequestQueue

我正在尝试为我的项目将参数从Android发送到PHP。

这是我发送参数的方式。 请注意,由于我不确定如何执行此操作,因此我尚未将json数组包括在CartRequest列表中。 另请注意,我不确定将ArrayList的GSON转换为Jason Array是否有效。 我想知道如何将其发送到PHP的原因是可以检查GSON转换是否确实有效

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);
    }

我想包括JSON数组cartitemscartRequest并对其进行排队。 我只需要Json数组cartitemsidquantity

这是我的CartRequest代码:

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;
    }
}

我将不胜感激。 提前致谢!

更新:添加循环后出现错误。

wrong 2nd argument type. Found: 'org.json.JSONARRAY', required: 'java.lang.String'

您必须使cartItems JSONArray JSONArray cartItems = new JSONArray(); 运行一个循环以将listProducts中的项目添加到ID和数量为每个数组项目的jsonobjects的cartItems中。

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请求

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);

}

请参阅http://www.androidhive.info/2014/09/android-json-parsing-using-volley/ http://www.androidhive.info/2014/05/android-working-with-volley-library-1 /

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM