Java 数组作为截击POST请求参数
我正在尝试使用数组作为参数发出截击POST请求,例如,我想要POSTJava 数组作为截击POST请求参数,java,android,arrays,android-volley,Java,Android,Arrays,Android Volley,我正在尝试使用数组作为参数发出截击POST请求,例如,我想要POST {"types":[1,2,3]} 我现在有一根绳子 {"types":"[1,2,3]"} 我就是这样提出截击请求的: JSONArray jsonArray = new JSONArray(); JSONObject jsonObject = new JSONObject(); try{ jsonObject.put("types", list); jsonArray.put
{"types":[1,2,3]}
我现在有一根绳子
{"types":"[1,2,3]"}
我就是这样提出截击请求的:
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
try{
jsonObject.put("types", list);
jsonArray.put(jsonObject);
System.out.println(jsonObject);
}catch(Exception e){
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>(){
@Override
public void onResponse(JSONObject response) {
Log.e("Response", response.toString());
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Error.Response", error.toString());
String json = null;
NetworkResponse response = error.networkResponse;
if(response != null && response.data != null){
switch(response.statusCode){
case 400:
json = new String(response.data);
System.out.println(json);
break;
}
}
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Authorization", Token);
return headers;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjectRequest);
JSONArray-JSONArray=new-JSONArray();
JSONObject JSONObject=新的JSONObject();
试一试{
jsonObject.put(“类型”,列表);
jsonArray.put(jsonObject);
System.out.println(jsonObject);
}捕获(例外e){
}
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.POST、url、jsonObject、,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Log.e(“Response”,Response.toString());
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.e(“Error.Response”,Error.toString());
字符串json=null;
NetworkResponse=错误。NetworkResponse;
if(response!=null&&response.data!=null){
开关(响应状态代码){
案例400:
json=新字符串(response.data);
System.out.println(json);
打破
}
}
}
})
{
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap headers=新的HashMap();
headers.put(“授权”,令牌);
返回标题;
}
};
RequestQueue RequestQueue=Volley.newRequestQueue(this);
add(jsonObjectRequest);
List是一个数组列表,声明为:
List<Integer> list = new ArrayList<Integer>();
List List=new ArrayList();
我假设jsonObject.put(“types”,list)将把我的数组变成一个列表,我应该如何解决这个问题?请尝试
JSONArray
而不是list
JSONArray types=new JSONArray();
types.put(1);
types.put(2);
types.put(3);
jsonObject.put("types", types);
jsonArray.put(jsonObject);
这将解决您的问题您需要从整数列表中创建一个
JSONArray
,并将其添加到jsonObject
很抱歉回复太晚,这正是问题所在谢谢@杰里科,我需要help@AdityaVyas-拉坎,我能帮你什么?