如何在排球中将JSON数组作为后期请求发送? [英] How to send json array as post request in volley?

查看:124
本文介绍了如何在排球中将JSON数组作为后期请求发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用volley进行json解析.我想使用POST将一些数据发送到服务器端.我正在尝试发送.现在任何人都可以告诉我如何将过滤器数组发送到服务器吗?

I am using volley for json parsing. I want to send some data using POST to server side. I am trying to send .Now can any one tell me that how can i send filter array to server?

以下是我的代码段.我也尝试了HashmapJsonobject.但出现此错误.

Following is my snippet code. i tried also Hashmap and Jsonobject. but getting this error.

错误:

org.json.JSONException: Value  at Data of type java.lang.String cannot be converted to JSONObject

格式

{
    "typeName": "MANUFACTURER",
    "typeId": 22,
    "cityId": 308,
    "sortBy": "productname",
    "sortOrder": "desc",
    "filter":[
                {
                    "filterId":101,
                    "typeName":"CAT_ID",

                     "filterId":102,
                    "typeName":"CAT_ID"
                }
             ]
}

用于代码检查粘贴

https://pastebin.com/u5qD8e2j

推荐答案

如果调用API时遇到问题,那么这将对您有所帮助.

If you are having a problem in calling the API then this will help you.

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

其中jObject是要发送到服务器的JSON数据.

where jObject is the JSON data you want to send to the server.

JSONArray的实现类似.代替JsonObjectRequest 使用JsonArrayRequest并发送jArray而不是jObject.

Implementation will be similar for JSONArray. Instead of JsonObjectRequest use JsonArrayRequest and send jArray instead of jObject.

要创建json数组,只需稍作调整

For creating json array just do a little tweak

JSONArray array=new JSONArray();

for(int i=0;i<filter_items.size();i++){
    JSONObject obj=new JSONObject();
    try {
        obj.put("filterId",filter_items.get(i));
        obj.put("typeName","CAT_ID");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    array.put(obj);
}

最后添加如下的json数组

And finally add json array as below

jsonParams.put("filter",array);

在您的情况下,您要将Json数组转换为字符串

In your case you are converting Json array to string

这篇关于如何在排球中将JSON数组作为后期请求发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆