How to send boolean or integer value using volleyball

I want to post boolean, double data using the volley library.I am not getting how to use it.Is there any other process.Thanks in advance.

Here is my method ....

@Override

        protected Map<String, String> getParams() {

            Map<String, String> params = new HashMap<String, String>();
            params.put("name", "name");
            params.put("email", "abc@abc.info");
            params.put("pass", "password");

            return params;
        }

      

+5


source to share


4 answers


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) {

      



+5


source


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 in a Json object.

+2


source


If you have a custom request, you can simply override the getBody method and go to city:

@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 content type as application / json in headers

0


source


Here is the full volley call request. You can change the method call. You can pass any type of parameters as a JSON object. You can set the 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'

      

0


source







All Articles