How to post boolean or integer value using volley

前端 未结 4 1620
予麋鹿
予麋鹿 2021-01-18 21:05

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....

         


        
相关标签:
4条回答
  • 2021-01-18 21:49
    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.

    0 讨论(0)
  • 2021-01-18 21:52
    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) {
    
    0 讨论(0)
  • 2021-01-18 21:52

    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

    0 讨论(0)
  • 2021-01-18 22:05

    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`
    
    0 讨论(0)
提交回复
热议问题