我想在Android中发送一个简单的POST请求,请求体应该等于以下内容:
问题在于getParams方法只接受Map对象,而我想要发送一个JSONArray。所以,我不得不使用强制转换,这会生成错误。我不知道该如何解决这个问题。谢谢。
[
{
"value": 1
}
]
我尝试在Android中使用Volley库,这是我的代码:
// the jsonArray that I want to POST
String json = "[{\"value\": 1}]";
JSONArray jsonBody = null;
try {
jsonBody = new JSONArray(json);
} catch (JSONException e) {
e.printStackTrace();
}
final JSONArray finalJsonBody = jsonBody;
// starting the request
final RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest request =
new JsonObjectRequest(com.android.volley.Request.Method.POST,"https://...",null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("mytag", "Response is: " + response);}},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Mytag", "error");}}) {
@Override
protected Map<String,String> getParams() {
// the problem is here...
return (Map<String, String>) finalJsonBody;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
// I put all my headers here like the following one :
params.put("Content-Type", "application/json");
return params;}};
queue.add(request);
问题在于getParams方法只接受Map对象,而我想要发送一个JSONArray。所以,我不得不使用强制转换,这会生成错误。我不知道该如何解决这个问题。谢谢。