I want to post boolean,double data using volley library.I am not getting how to use it.Is there any other process.Thanks in advance.
Here is my method....
JSONObject object = new JSONObject();
try {
object.put("compression", false);
object.put("instructions", true);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, API_URL, object, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
parseDirectionsData(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<>();
params.put("loc", "-33.9717974,18.6029783");
return params;
}
};
Any of the parameters that are not Strings you can wrap them into the Json Object.
JSONObject obj = new JSONObject();
obj.put("isboolean",false)
JsonObjectRequest req = new JsonObjectRequest(Constants.URL_PATH, obj,
new Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
If you have a custom Request, you could just override the getBody method and go to town:
@Override
public byte[] getBody(){
JSONObject jsonObject = new JSONObject();
String body = null;
try{
//Here are your parameters:
jsonObject.put("name", "geronimous");
jsonObject.put("age", 998);
jsonObject.put("happy", true);
body = jsonObject.toString();
} catch (JSONException e){
e.printStackTrace();
}
try{
return body.toString().getBytes("utf-8");
} catch (UnsupportedEncodingException e){
e.printStackTrace();
}
return null;
}
Just make sure you have the content type as application/json on the headers
Here is the complete request of a volley call. You can change the method call. You can pass any type of parameters as a JSON object You can set request header
JSONObject jsonObject = new JSONObject();
jsonObject.put("stringValue", "abc");
jsonObject.put("doubleValue", 13.066);
jsonObject.put("integerValue", 120);
jsonObject.put("booleanValue", true);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, getString(R.string.api_url), jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
getJsonResult(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
) {
@Override //Send Header
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("api_call_header", "header_value");
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(request);`enter code here`