使用 volley 向 android 发送 Json 数组 POST 请求 [英] Send a Json array POST request with android using volley
问题描述
我正在开发应用程序,在该应用程序中我使用 volley 将数据发送到服务器,现在我想以 json 数组的形式将数据发送到服务器,但不知道如何在数组中发送??
i am developing application in which i send data to server using volley now i want to send data to server in json array but not know how to send in array??
Map<String, String> postParam = new HashMap<String, String>();
postParam.put("Token", "U2FsdGVkX13CFEM=");
postParam.put("AppDB", "day");
postParam.put("ContentTypeName", "Users");
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
AppConfig.URL_REGISTER_WITHOUT_IMG, new JSONObject(postParam),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
// msgResponse.setText(response.toString());
// hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
// hideProgressDialog();
}
}) {
/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
jsonObjReq.setTag(TAG);
// Adding request to request queue
Volley.newRequestQueue(this).add(jsonObjReq);
}
现在我想以预期的json数组发送数据{"AppDb":"天","ContentTypeName":"用户","令牌":"U2FsdGVkX13CFEM=",过滤数据":[{"FilterName":"EmailId","FilterValue":"通过用户在此处输入电子邮件进行检查"}]
now i want to send data in array expected json { "AppDb":"day", "ContentTypeName":"Users", "Token":"U2FsdGVkX13CFEM=", "FilterData":[ {"FilterName":"EmailId","FilterValue":" Pass the user enter email here to check"} ]
}
推荐答案
试试这个:
JSONArray jsonArray=new JSONArray();
JSONObject jsonObj=new JSONObject();
try {
jsonObj.put("filterName","EmailId");
jsonObj.put("FilterValue",// user email);
} catch (JSONException e) {
e.printStackTrace();
}
jsonArray.put(jsonObj);
像这样将其添加到您的参数中:
Add it to your params like this:
postParam.put("FilterData",jsonArray);
这篇关于使用 volley 向 android 发送 Json 数组 POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!