Volley Post请求,在Json数组请求中发送Json对象

编程入门 行业动态 更新时间:2024-10-27 22:24:21
本文介绍了Volley Post请求,在Json数组请求中发送Json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

简单地说,我想使用Volley Post Request将此{"Id":7,"Name":"MyName"}数据发送到服务器.

To say in simple words i want to send this {"Id":7,"Name":"MyName"} data To server using Volley Post Request.

它有1个整数和1个字符串,我得到的响应是Jsonarray

It has 1 integer and 1 String and response i get is Jsonarray

我尝试了以下方法,但没有用

I tried Following ways but none are working

  • 因为它是json数组请求,所以我无法在参数中发送数据,因为第三个参数仅使用JsonArray,并且我必须发送JsonObject,因此将其保留为空

  • as it is json array request i cannot send data in argument as 3rd argument only takes JsonArray and i have to send JsonObject so kept it as null

new JsonArrayRequest(Method,Url,JsonArray,ResponseListener,ErrorListner)

我不能将其放在HashMap中,因为该值的1是整数,并且仅接受字符串

I cannot put it in HashMap as 1 of the value is integer, and it only accepts string

getparams()方法

getparams() method

@Override protected Map<String, String> getParams() throws AuthFailureError { Map<String,String> params=new HashMap<>(); params.put("Id",7); // <====== This is Invalid params.put("Name","MyName"); return params; }

  • 我尝试发送getbody方法,但仍然无法正常工作
  • getbody方法

    @Override public byte[] getBody() { String body="{\"Id\":7,\"Name\":\"MyName\"}"; return body.getBytes(); }

    我可以使用HttpUrlConnection获得响应.

    I can get the response using HttpUrlConnection.

    还有其他方法可以实现凌空抽射吗?

    Is there any other way to achieve it in volley ?

    推荐答案

    似乎在最近的 volley版本,但是您可以轻松地修改此构造函数并将其添加到JsonArrayRequest中.

    Seems like it was removed in recent volley version but you can easily modify this constructor and add to JsonArrayRequest.

    public JsonArrayRequest(int method, String url, JSONObject jsonRequest, Listener<JSONArray> listener, ErrorListener errorListener) { super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener); }

更多推荐

Volley Post请求,在Json数组请求中发送Json对象

本文发布于:2023-10-10 12:19:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478651.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   对象   Volley   Post   Json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!