【问题标题】:Android Volley Multidimensional JSON Request post MethodAndroid Volley 多维 JSON 请求发布方法
【发布时间】:2021-08-09 11:33:18
【问题描述】:

如何在 volley post 方法中发送多维数组。

    {
  "code": "001",
    "Emp_id": "0000",
    "Exp_Dt": "2021-05-27T00:00:00.000",
    "users": [
        {
                "id": "1087",
                "name": "Abhishek Saini",
                "email": "info@ezacake.com",
                "gender" : "male"
        },
        {
                "id": "1088",
                "name": "Gourav",
                "email": "gourav9188@gmail.com",
                "gender" : "male"
                
        }
  ]
}

这种格式我想调用API。请帮忙 注意 - 我想在我的 API 上使用数组请求,而不是响应

【问题讨论】:

    标签: android arrays json http-post android-volley


    【解决方案1】:

    您好,您正在尝试在那里发送一个 json 对象 =>

    创建一个父 json 对象

    JSONObject reqJO = new JSONObject();
    

    输入其他属性,例如

    reqJo.put("code",0001);
    

    创建一个用户json对象

    JSONObject user = new JSONObject();
    

    放置用户属性(如果更多用户循环创建新用户)例如

    user.put("id",1087)
    

    创建一个用户的 json 数组

    JSONArray users = new JSONArray();
    

    在用户数组中添加用户

    users.put(user);
    

    将用户数组放入父对象中

    reqJO.put("users",users);
    
    

    然后你继续你的JsonObjectRequest

    【讨论】:

    • 谢谢@Lloyd T Mashumba,我创建了一个 JSON 数组。我尝试发送显示 .invalid JSON 数组的值。也许它传递方法错误。请检查此代码。如何发送 JSON 数组。 code 覆盖有趣的 getParams(): Map? { val params: MutableMap = HashMap() reqJO.put("users",arrayPayLoadRe) return params } code
    • json 数组在对象内部...尝试 jsonobjectrequest => new JsonObjectRequest(url,jsonObject,response -> {},error -> {});
    【解决方案2】:

    这就是答案

    RequestQueue queue = Volley.newRequestQueue(this);
    JsonObjectRequest jobReq = new JsonObjectRequest(Request.Method.POST, url, jObject,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
    
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
    
                }
            });
    
    queue.add(jobReq);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多