【问题标题】:How to Send multiple Json objects by using volley in android如何在 android 中使用 volley 发送多个 Json 对象
【发布时间】:2016-08-08 06:37:35
【问题描述】:

我想在 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;
        }

    };

}

【问题讨论】:

  • 使用 JSONArray 您可以发送多个 JSONObject 例如:多个 JSONObject 在一个 JSONArray 中,然后 JSONArray 传递到您的 API

标签: android json post android-volley


【解决方案1】:

糟糕的编程习惯。

这些 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"
}]

}

【讨论】:

  • 你能告诉我如何将它传递给一个api吗
  • 如何发送这个json数据你知道吗?
  • 我只能发送一次意味着第一个对象
  • 如果有人有此代码,请分享它,我有同样的问题,我无法获取响应值
猜你喜欢
  • 2021-01-26
  • 1970-01-01
  • 2015-04-05
  • 1970-01-01
  • 2014-11-06
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
  • 1970-01-01
相关资源
最近更新 更多