如何在android中使用volley发送多个Json对象

时间:2022-10-22 20:41:44

I want to send this json data using post method in volley here is multiple json objects with different tag name

我想在volley中使用post方法发送这个json数据,这里有多个具有不同标记名称的json对象

 [{"name":"hi","address":"home","Language":"English"},
 {"name":"hello","address":"house","Language":"English"},
 {"name":"man","address":"India","Language":"Hindi"}]

Below is my working code here i'm sending single json objects , can anyone help me. Thanks in advance.

下面是我的工作代码,我发送单个json对象,任何人都可以帮助我。提前致谢。

      private void sendMessage() {

     final Map<String, String> params = new HashMap<String, String>();
    String tag_json_obj = "json_obj_req";
    //String url = "http://192.168.0.106:59181/api/Employees";
    String url = "http://android.azurewebsites.net/kfdgf/Employees";

    final ProgressDialog pDialog = new ProgressDialog(this);
    pDialog.setMessage("Senting message...");
    pDialog.show();

    StringRequest req = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {

                    pDialog.hide();

                    pDialog.hide();
                    // Toast.makeText(getApplicationContext(),"hi", Toast.LENGTH_SHORT).show();
                    Log.d("", response);

                    finish();


                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("", "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
            pDialog.hide();

            // hide the progress dialog

        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();

            params.put("name", name);

            params.put("address", agentId);
            params.put("Language", count);
            return params;
        }

    };

}

1 个解决方案

#1


2  

Bad programming practice.

糟糕的编程习惯。

These JSON objects can send one by one only, by looping the service calls.

这些JSON对象只能通过循环服务调用逐个发送。

if you want to send this JSON object in one shot then follow some standard structure of JSON. Create and JSON array of objects and pass it to api in one shot.

如果你想一次发送这个JSON对象,那么遵循一些标准的JSON结构。创建和JSON对象数组并一次性将其传递给api。

standard JSON format to pass multiple objects into single shot,

将多个对象传递到单个镜头的标准JSON格式,

{
"data": [{
    "name": "hi",
    "address": "home",
    "Language": "English"
}, {
    "name": "hello",
    "address": "house",
    "Language": "English"
}, {
    "name": "man",
    "address": "India",
    "Language": "Hindi"
}]

}

}

#1


2  

Bad programming practice.

糟糕的编程习惯。

These JSON objects can send one by one only, by looping the service calls.

这些JSON对象只能通过循环服务调用逐个发送。

if you want to send this JSON object in one shot then follow some standard structure of JSON. Create and JSON array of objects and pass it to api in one shot.

如果你想一次发送这个JSON对象,那么遵循一些标准的JSON结构。创建和JSON对象数组并一次性将其传递给api。

standard JSON format to pass multiple objects into single shot,

将多个对象传递到单个镜头的标准JSON格式,

{
"data": [{
    "name": "hi",
    "address": "home",
    "Language": "English"
}, {
    "name": "hello",
    "address": "house",
    "Language": "English"
}, {
    "name": "man",
    "address": "India",
    "Language": "Hindi"
}]

}

}