简体   繁体   中英

How to Send multiple Json objects by using volley in android

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

 [{"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.

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

    };

}

Bad programming practice.

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

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.

standard JSON format to pass multiple objects into single shot,

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

}

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.

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