简体   繁体   English

使用Volley将数据列表发送到服务器

[英]Send data list to server using Volley

I need to pass data list to server using volley and access that data using PHP. 我需要使用排球将数据列表传递到服务器,并使用PHP访问该数据。 From android application I need to pass all the data in one table and access that data on server. 从Android应用程序中,我需要将所有数据传递到一个表中并在服务器上访问该数据。 You can think of it as a passing data in shopping cart to the server. 您可以将其视为将购物车中的数据传递到服务器。

I can get the data from database as a list. 我可以从数据库中获取数据作为列表。 I need to submit data to server and access it. 我需要将数据提交到服务器并访问它。

RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, NetworkHandler.ORDER, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        try{
            Log.d(LogMessages.ORDER_CONFIRM,response.toString());
        }
        catch(Exception ex){
            Toast.makeText(getApplicationContext(), ex.toString(), Toast.LENGTH_SHORT).show();
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show();
    }
}) {
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        List<Food> listCart = db.getAllCartItems();
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("orderitems",listCart .toString());
        return parameters;
    }
};
request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES*2, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(request);

If you mean to send variant length list of data to server, generally I will do this: 1. send a "count" parameter to indicate how many records 2. send "record_1", "record_2".... parameters to server 如果您打算将可变长度的数据列表发送到服务器,通常我会这样做:1.发送“ count”参数以指示有多少条记录2.将“ record_1”,“ record_2” ....参数发送到服务器

On server side you should check "count" first then you check "recored_#" from 1 to the count you just get. 在服务器端,您应该先检查“ count”,然后再检查“ recored_#”从1到刚得到的计数。

You can use json format to send multiple data to server and use gson to convert to json array 您可以使用json格式将多个数据发送到服务器,并使用gson转换为json数组

Gson gson = new GsonBuilder().create();
JsonArray myCustomArray = gson.toJsonTree(listCart ).getAsJsonArray();

I found a solution . 我找到了解决方案。 I used GSON library. 我使用了GSON库。 This is solution. 这是解决方案。

List<Food> listCart = db.getAllCartItems();
                             Gson gson = new Gson();
                             String data = gson.toJson(listCart);
                             Log.d(LogMessages.ORDER_CONFIRM,data);
                             Map<String, String> parameters = new HashMap<String, String>();
                             parameters.put("orderitems",data);

In the PHP side i get the orderitems using $_POST['orderitems'] and decoded that data using json_decode.Then I can access each object. 在PHP方面,我使用$ _POST ['orderitems']获取订单项,并使用json_decode对该数据进行解码。然后,我可以访问每个对象。

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

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