I'm trying to make a request to MVC Server with web-api using volley in android, I want to send the parameters of the function as a JSON (the request body), so I used JSONRequest to make the call but the problem is the JSONRequest forces you to Use the Response.Listner, and the function I'm calling returns a Boolean variable , so I'm getting casting error every time I make the call
I tried to make a custom call that accepts Boolean but now I'm getting error 400 and I don't know why, is there a way to make the jsonRequest receive Boolean? and if not, what is the solution ?
the function I used the first time:
public void getJSON(String controllerName,String actionName, JSONObject requestBody,Response.Listener<JSONObject> success,Response.ErrorListener error){
String url = makeURL(controllerName,actionName);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,url,requestBody,success,error);
request.setTag(APP_TAG);
requestQueue.add(request);
}
and here is my second trial with custom request (getting error 400):
public void getBooleanRequest(String controllerName,String actionName, final HashMap<String,String> body, final HashMap<String,String> header, final Response.Listener<Boolean> success,Response.ErrorListener error){
String url = makeURL(controllerName,actionName);
Request<Boolean> request = new Request<Boolean>(Request.Method.POST,url,error) {
@Override
protected Response<Boolean> parseNetworkResponse(NetworkResponse response) {
return Response.success(true, HttpHeaderParser.parseCacheHeaders(response));
}
@Override
protected void deliverResponse(Boolean response) {
success.onResponse(response);
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return body;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return header;
}
};
request.setTag(APP_TAG);
requestQueue.add(request);
}
You can try my following sample code:
private class BooleanRequest extends Request<Boolean> {
private final Response.Listener<Boolean> mListener;
private final Response.ErrorListener mErrorListener;
private final String mRequestBody;
private final String PROTOCOL_CHARSET = "utf-8";
private final String PROTOCOL_CONTENT_TYPE = String.format("application/json; charset=%s", PROTOCOL_CHARSET);
public BooleanRequest(int method, String url, String requestBody, Response.Listener<Boolean> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.mListener = listener;
this.mErrorListener = errorListener;
this.mRequestBody = requestBody;
}
@Override
protected Response<Boolean> parseNetworkResponse(NetworkResponse response) {
Boolean parsed;
try {
parsed = Boolean.valueOf(new String(response.data, HttpHeaderParser.parseCharset(response.headers)));
} catch (UnsupportedEncodingException e) {
parsed = Boolean.valueOf(new String(response.data));
}
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
return super.parseNetworkError(volleyError);
}
@Override
protected void deliverResponse(Boolean response) {
mListener.onResponse(response);
}
@Override
public void deliverError(VolleyError error) {
mErrorListener.onErrorResponse(error);
}
@Override
public String getBodyContentType() {
return PROTOCOL_CONTENT_TYPE;
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
mRequestBody, PROTOCOL_CHARSET);
return null;
}
}
}
Then in your Activity:
try {
JSONObject jsonBody;
jsonBody = new JSONObject();
jsonBody.put("Title", "Android Demo");
jsonBody.put("Author", "BNK");
jsonBody.put("Date", "2015/08/28");
String requestBody = jsonBody.toString();
BooleanRequest booleanRequest = new BooleanRequest(0, url, requestBody, new Response.Listener<Boolean>() {
@Override
public void onResponse(Boolean response) {
Toast.makeText(mContext, String.valueOf(response), Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(mContext, error.toString(), Toast.LENGTH_SHORT).show();
}
});
// Add the request to the RequestQueue.
queue.add(booleanRequest);
} catch (JSONException e) {
e.printStackTrace();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With