我正在使用volley进行json解析。我想使用POST向服务器端发送一些数据。我想发送。现在任何人都可以告诉我如何将过滤器阵列发送到服务器?
以下是我的代码段。我也尝试了Hashmap
和Jsonobject
。但是得到了这个错误。
错误:
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"
}
]
}
代码检查贴纸
答案 0 :(得分:8)
如果您在调用API时遇到问题,那么这将对您有所帮助。
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数据。
JSONArray的实现类似。而不是JsonObjectRequest 使用JsonArrayRequest并发送jArray而不是jObject。
为了创建json数组,只需做一点调整
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数组,如下所示
jsonParams.put("filter",array);
在您的情况下,您将Json数组转换为字符串
答案 1 :(得分:2)
{
"typeName": "MANUFACTURER",
"typeId": 22,
"cityId": 308,
"sortBy": "productname",
"sortOrder": "desc",
"filter":[
{
"filterId":101,
"typeName":"CAT_ID",
}
{
"filterId":102,
"typeName":"CAT_ID"
}
]
}
JSONObject object=new JSONObject();
object.put("typeName","");
object.put("typeId","");
object.put("cityId","");
object.put("sortBy","");
object.put("sortOrder","");
JSONArray array=new JSONArray();
JSONObject obj=new JSONObject();
obj.put("filterId","");
obj.put("typeName","");
array.put(obj);
object.put("filter",obj.toString());
传递JSONObject以发出请求。使用此https://www.androidhive.info/2014/09/android-json-parsing-using-volley/
答案 2 :(得分:2)
希望这会对你有所帮助。
//Create Main jSon object
JSONObject jsonParams = new JSONObject();
try {
//Add string params
jsonParams.put("typeName", "MANUFACTURER");
jsonParams.put("typeId", "22");
jsonParams.put("cityId", "308");
jsonParams.put("sortBy", "productname");
jsonParams.put("sortOrder", "desc");
} catch (JSONException e) {
e.printStackTrace();
}
//Create json array for filter
JSONArray array=new JSONArray();
//Create json objects for two filter Ids
JSONObject jsonParam1 =new JSONObject();
JSONObject jsonParam2 =new JSONObject();
try {
jsonParam1.put("filterId","101");
jsonParam1.put("typeName","CAT_ID");
jsonParam2.put("filterId","102");
jsonParam2.put("typeName","CAT_ID");
} catch (JSONException e) {
e.printStackTrace();
}
//Add the filter Id object to array
array.put(jsonParam1);
array.put(jsonParam2);
//Add array to main json object
try {
jsonParams.put("filter",array);
} catch (JSONException e) {
e.printStackTrace();
}
有关如何创建json对象的更多信息,请查看此链接
Android JSONObject : add Array to the put method
修改强>:
如果有更多数据,最好使用Gson转换器
http://www.vogella.com/tutorials/JavaLibrary-Gson/article.html
另外,对于创建pojo类,请使用此
答案 3 :(得分:2)
您好Volley不支持JsonArray请求更好地使用其他一些库...
答案 4 :(得分:1)
我使用下面的代码将JSONArray发布到排球。您必须使用JsonArrayRequest并直接传递JSON数组,而不将其添加到任何JSONObject。 还要记住覆盖“parseNetworkResponse”方法以再次将响应转换为JSONArray,因为JsonArrayRequest的ResponseListner需要一种类型的JSONArray
String URL = "www.myposturl.com/data";
RequestQueue queue = Volley.newRequestQueue(this);
//Create json array for filter
JSONArray array = new JSONArray();
//Create json objects for two filter Ids
JSONObject jsonParam = new JSONObject();
JSONObject jsonParam1 = new JSONObject();
try {
//Add string params
jsonParam.put("NAME", "XXXXXXXXXXXXXX");
jsonParam.put("USERNAME", "XXXXXXXXXXXXXX");
jsonParam.put("PASSWORD", "XXXXXXXXXXXX");
jsonParam1.put("NAME", "XXXXXXXXXXXXXX");
jsonParam1.put("USERNAME", "XXXXXXXXXXXXXX");
jsonParam1.put("PASSWORD", "XXXXXXXXXXXX");
} catch (JSONException e) {
e.printStackTrace();
}
array.put(jsonParam);
array.put(jsonParam1);
JsonArrayRequest request_json = new JsonArrayRequest(Request.Method.POST, URL, array,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Get Final response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
VolleyLog.e("Error: ", volleyError.getMessage());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
// Add headers
return headers;
}
//Important part to convert response to JSON Array Again
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
String responseString;
JSONArray array = new JSONArray();
if (response != null) {
try {
responseString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
JSONObject obj = new JSONObject(responseString);
(array).put(obj);
} catch (Exception ex) {
}
}
//return array;
return Response.success(array, HttpHeaderParser.parseCacheHeaders(response));
}
};
queue.add(request_json);
答案 5 :(得分:0)
遵循以下三个步骤应该可以使缺少此支持的旧Volley库正常工作。
准备有效载荷并发布:
JSONArray payloadItems = new JSONArray();
JSONObject payloadItem1=new JSONObject();
//set properties on item1
payloadItem1.put('prop1',"val11");
payloadItems.put(payloadItem1);
JSONObject payloadItem2=new JSONObject();
//set properties on item1
payloadItem2.put('prop1',"val12");
payloadItems.put(payloadItem1);
JsonArrayRequest request;
request = new JsonArrayRequest(Request.Method.POST,url,payloadItems, new Response.Listener<JSONArray>() {
@SuppressWarnings("unchecked")
@Override
public void onResponse(JSONArray response) {
//your logic to handle response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//your logic to handle error
}
}) {
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
/* This is very important to pass along */
params.put("Content-Type","application/json");
//other headers if any
return params;
}
};
request.setRetryPolicy(new DefaultRetryPolicy(10000, 2, 2));
VolleyHelper.init(this);
VolleyHelper.getRequestQueue().add(request);
[如果需要]如果尚未在Volley包的Class- JsonArrayRequest中添加此构造函数,则
public JsonArrayRequest(int method, String url, JSONArray jsonArray, Listener<JSONArray> listener, ErrorListener errorListener) {
super(method, url, (jsonArray == null) ? null : jsonArray.toString(),
listener, errorListener);
}
[如果需要]如果尚未实现以支持服务器的JSONArray响应,则重写此方法。
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
String responseString;
JSONArray array = new JSONArray();
if (response != null) {
try {
responseString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
JSONObject obj = new JSONObject(responseString);
(array).put(obj);
} catch (Exception ex) {
}
}
//return array;
return Response.success(array, HttpHeaderParser.parseCacheHeaders(response));
}