Post Json Object Data To Get Json Array Response Using Volley In Android
I need to post JSONObject (using Volley) to a web-service which is returning the response in JSONArray format. Here is what I have tried so far. final JSONObject requestJsonObjec
Solution 1:
Below helper class has fixed my problem
publicclassCustomRequestextendsJsonRequest<JSONArray> {
protectedstatic final StringPROTOCOL_CHARSET = "utf-8";
/**
* Creates a new request.
* @param method the HTTP method to use
* @param url URL to fetch the JSON from
* @param requestBody A {@link String} 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.
*/publicCustomRequest(int method, String url, String requestBody, Listener<JSONArray> listener, ErrorListener errorListener) {
super(method, url, requestBody, listener, errorListener);
}
/**
* Creates a new request.
* @param url URL to fetch the JSON from
* @param listener Listener to receive the JSON response
* @param errorListener Error listener, or null to ignore errors.
*/publicCustomRequest(String url, Listener<JSONArray> listener, ErrorListener errorListener) {
super(Method.GET, url, null, listener, errorListener);
}
/**
* Creates a new request.
* @param method the HTTP method to use
* @param url URL to fetch the JSON from
* @param listener Listener to receive the JSON response
* @param errorListener Error listener, or null to ignore errors.
*/publicCustomRequest(int method, String url, Listener<JSONArray> listener, ErrorListener errorListener) {
super(method, url, null, listener, errorListener);
}
/**
* Creates a new request.
* @param method the HTTP method to use
* @param url URL to fetch the JSON from
* @param jsonRequest A {@link JSONArray} 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.
*/publicCustomRequest(int method, String url, JSONArray jsonRequest, Listener<JSONArray> listener, ErrorListener errorListener) {
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener);
}
/**
* 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.
*/publicCustomRequest(int method, String url, JSONObject jsonRequest, Listener<JSONArray> listener, ErrorListener errorListener) {
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener);
}
/**
* Constructor which defaults to <code>GET</code> if <code>jsonRequest</code> is
* <code>null</code>, <code>POST</code> otherwise.
*
* @see #MyjsonPostRequest(int, String, JSONArray, Listener, ErrorListener)
*/publicCustomRequest(String url, JSONArray jsonRequest, Listener<JSONArray> listener, ErrorListener errorListener) {
this(jsonRequest == null ? Method.GET : Method.POST, url, jsonRequest, listener, errorListener);
}
/**
* Constructor which defaults to <code>GET</code> if <code>jsonRequest</code> is
* <code>null</code>, <code>POST</code> otherwise.
*
* @see #MyjsonPostRequest(int, String, JSONObject, Listener, ErrorListener)
*/publicCustomRequest(String url, JSONObject jsonRequest, Listener<JSONArray> listener, ErrorListener errorListener) {
this(jsonRequest == null ? Method.GET : Method.POST, url, jsonRequest, listener, errorListener);
}
@OverrideprotectedResponse<JSONArray> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = newString(response.data, HttpHeaderParser.parseCharset(response.headers));
returnResponse.success(newJSONArray(jsonString), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
returnResponse.error(newParseError(e));
} catch (JSONException je) {
returnResponse.error(newParseError(je));
}
}
}
How to use this?
JSONObject requestJsonObject = newJSONObject();
requestJsonObject.put("first_name", firstName);
requestJsonObject.put("last_name", lastName);
requestJsonObject.put("email_address", emailId);
requestJsonObject.put("password", password);
CustomRequest jsonObjReq = newCustomRequest(Method.POST, YOUR_URL, requestJsonObject, responseListener, errorListener);
Solution 2:
Looks like you are getting the json array response.
Can you change your code like this.
privateListener<JsonArray> loginResponseListener = newListener<JsonArray>() {
@OverridepublicvoidonResponse(JsonArray resposne) {
//other stuff goes here
}
};
Solution 3:
try this one
RequestQueue request = Volley.newRequestQueue(this);
JsonArrayRequest jsonArrayRequest = newJsonArrayRequest(url,newResponse.Listener<JSONArray>()
{
@OverridepublicvoidonResponse(JSONArray response)
{
List<Contact> result = newArrayList<Contact>();
for (int i = 0; i < response.length(); i++)
{
try
{
result.add(convertContact(response
.getJSONObject(i)));
}
catch (JSONException e)
{
}
}
adpt.setItemList(result);
adpt.notifyDataSetChanged();
}
}, newResponse.ErrorListener()
{
@OverridepublicvoidonErrorResponse(VolleyError error)
{
// Handle error
}
});
request.add(jsonArrayRequest);
Post a Comment for "Post Json Object Data To Get Json Array Response Using Volley In Android"