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

时间:2016-08-08 06:37:36

标签: android json post android-volley

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

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

下面是我的工作代码,我发送单个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 个答案:

答案 0 :(得分:2)

糟糕的编程习惯。

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

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

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

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

}