由网友(趁微风正好)分享简介:我想送一个新的 JsonObjectRequest 要求:我想收到JSON数据(从服务器响应):确定我想这一请求向服务器发送JSON格式的数据。I want to receive JSON data (response from server): OKI want to send JSON formatted dat...
我想送一个新的 JsonObjectRequest
要求:
我想这一请求向服务器发送JSON格式的数据。 I want to receive JSON data (response from server): OK
I want to send JSON formatted data with this request to the server
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST, "myurl.com", null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//...
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//...
}
})
{
@Override
protected Map<String,String> getParams() {
// something to do here ??
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
// something to do here ??
return params;
}
};
P.S。我用在我的项目也GSON库。
P.S. I use GSON library in my project too.
推荐答案
JsonObjectRequest
实际上接受的JSONObject
之身。
从这篇博客文章,
final String url = "some/url";
final JSONObject jsonBody = new JSONObject("{"type":"example"}");
new JsonObjectRequest(url, jsonBody, new Response.Listener<JSONObject>() { ... });
下面是source code和JavaDoc的( @参数jsonRequest
):
/**
* Creates a new request.
* @param method the HTTP method to use
* @param url URL to fetch the JSON from
* @param jsonRequest A {@link JSONObject} to post with the request. Null is allowed and
* indicates no parameters will be posted along with request.
* @param listener Listener to receive the JSON response
* @param errorListener Error listener, or null to ignore errors.
*/
public JsonObjectRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONObject> listener, ErrorListener errorListener) {
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
errorListener);
}
相关推荐
最新文章