我如何发送一个JSONObject和recive一个JsonArray [英] How I send a JsonObject and recive a JsonArray
问题描述
当我测试凌空图书馆,一个疑问产生了我关于法后。
的事情是,我一直在工作的JSONObject到现在为止,对于我用下面的code和它的作品。在这种方法我发送和的JSONObject和接收另一个JsonObjet,我没有问题的。
现在我的疑问是,我该怎么做派一个JSONObject并接收ArrayObject的?
我真的失去了这一点,我真的会AP preciate你的帮助,在此先感谢
公共无效testPost(字符串URL,最后的字符串标记,JSONObject的OBJ){
//最后ProgressDialog pDialog =新ProgressDialog(背景);
//pDialog.setMessage(\"Loading ...);
//pDialog.show();
JsonObjectRequest jsonObjReqpost =新JsonObjectRequest(Request.Method.POST,
URL,OBJ,
新Response.Listener<&JSONObject的GT;(){
@覆盖
公共无效onResponse(JSONObject的响应){
//测试
Log.d(SendRequestJsonPost,response.toString());
//pDialog.hide();
} },新Response.ErrorListener(){ @覆盖
公共无效onErrorResponse(VolleyError错误){
如果(error.networkResponse = NULL&放大器;!&安培;!error.networkResponse.data = NULL){
错误=新VolleyError(新的String(error.networkResponse.data));
}
字符串失败= handlingErrors.resolverErrors(error.toString());
//Log.d(\"SendRequestJsonPost,失败);
//pDialog.hide();
}
}){
//头
@覆盖
公共地图<字符串,字符串> getHeaders()抛出AuthFailureError {
HashMap的<字符串,字符串>标题=新的HashMap<>();
headers.put(内容类型,应用/ JSON);
headers.put(字符集,UTF-8);
返回头;
}
};
jsonObjReqpost.setTag(标签);
Singleton.getInstance(上下文).addToRequestQueue(jsonObjReqpost);
}
您可以使用下面的方法来获取 JsonArray
作为响应。
公共JsonArrayRequest(INT方法,字符串URL,JSONObject的jsonRequest,
听者LT; JSONArray>监听器,ErrorListener errorListener)
{
?超(方法,URL,(jsonRequest == NULL)空:jsonRequest.toString()
监听器,errorListener);
}
请参见这个答案了解更多详情。
While I was testing the "volley" library, a doubt arose me regarding the method "post".
The thing is that I have been working with JsonObject until now, for that I used the following code and it works. In this method I send and JsonObject and receive another JsonObjet, I had no problem with it.
Now my doubt is, How can I do to send a JsonObject and receive an ArrayObject?
I am really lost with this, I will really appreciate your help, thanks in advance
public void testPost(String url, final String tag, JSONObject obj) {
//final ProgressDialog pDialog = new ProgressDialog(context);
//pDialog.setMessage("Loading...");
//pDialog.show();
JsonObjectRequest jsonObjReqpost = new JsonObjectRequest(Request.Method.POST,
url, obj,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//test
Log.d("SendRequestJsonPost", response.toString());
//pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error.networkResponse != null && error.networkResponse.data != null) {
error = new VolleyError(new String(error.networkResponse.data));
}
String fail = handlingErrors.resolverErrors(error.toString());
//Log.d("SendRequestJsonPost", fail);
//pDialog.hide();
}
}) {
//header
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("charset", "utf-8");
return headers;
}
};
jsonObjReqpost.setTag(tag);
Singleton.getInstance(context).addToRequestQueue(jsonObjReqpost);
}
You can use the following method to get JsonArray
as response.
public JsonArrayRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONArray> listener, ErrorListener errorListener)
{
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(),
listener, errorListener);
}
Please refer this answer for more details.
这篇关于我如何发送一个JSONObject和recive一个JsonArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!