0

I am making a volley request and trying to POST an integer, following this solution Send Int in HashMap with Volley my final code looks like this:

        JSONArray jsonArray = new JSONArray();
    JSONObject jsonObject = new JSONObject();

    try{
        jsonObject.put("store",storeId);
        jsonObject.put("people", people);
        jsonObject.put("date",reserveDate);
        jsonObject.put("time", reserveTime);
        jsonArray.put(jsonObject);

        Log.i("jsonString", jsonObject.toString());


    }catch(Exception e){

    }


    StringRequest stringRequest = new StringRequest(Request.Method.POST, domain + api,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    Log.e("Response", response);

                    return;
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    Log.e("Error.Response", error.toString());
                    String json = null;

                    NetworkResponse response = error.networkResponse;
                    if(response != null && response.data != null){
                        switch(response.statusCode){
                            case 400:
                                json = new String(response.data);
                                System.out.println(json);
                                //json = trimMessage(json, "message");
                                //if(json != null) displayMessage(json);
                                break;
                        }
                        //Additional cases
                    }

                }

            })

    {
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put("store",storeId);
            params.put("people",String.valueOf(people));
            params.put("date", "2017-01-04");
            params.put("time", reserveTime);
            Log.d("params", params.toString());
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("Authorization", finalToken);
            Log.d("headers", headers.toString());
            return headers;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

Here I want to POST "people" as an integer but the error message I get is that "people" is a string value, I had declared "people" as :

 Integer people = Integer.parseInt(reserveNum);

where reserveNum is a number value from edittext. how do i make "peopel" in my request an integer?

UPDATE error message is :

{"errors":[{"message":"Invalid type: string (expected integer)","params":{"type":"string","expected":"integer"},"code":0,"dataPath":"/people","schemaPath":"/properties/people/type",.....
Community
  • 1
  • 1
JerryKo
  • 384
  • 7
  • 25

3 Answers3

2

I have figured the solution by changing Stringrequest to JSONObject reqeust. By doing so my request will be in a JSON format containing integers instead of strings.

JSONArray jsonArray = new JSONArray();
    JSONObject jsonObject = new JSONObject();

    try{
        jsonObject.put("store",storeId);
        jsonObject.put("people", people);
        jsonObject.put("date",reserveDay);
        jsonObject.put("time", reserveTime);
        jsonArray.put(jsonObject);
        Log.i("jsonString", jsonObject.toString());


    }catch(Exception e){

    }

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, domain + api, jsonObject,
            new Response.Listener<JSONObject>(){
                @Override
                public void onResponse(JSONObject response) {
                    Log.e("Response", response.toString());
                    try {
                        JSONArray arrData = response.getJSONArray("data");
                        parseData(arrData);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("Error.Response", error.toString());
                    String json = null;
                    NetworkResponse response = error.networkResponse;
                    if(response != null && response.data != null){
                        switch(response.statusCode){
                            case 400:

                                json = new String(response.data);
                                System.out.println(json);
                                break;
                        }
                        //Additional cases
                    }
                }
            })
    {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("Authorization", finalToken);
        Log.d("headers", headers.toString());
            return headers;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonObjectRequest);
}
JerryKo
  • 384
  • 7
  • 25
0

try the following code replacing above code:

    JSONArray jsonArray = new JSONArray();
    JSONObject jsonObject = new JSONObject();

    try{
        jsonObject.put("store",storeId);
        //people is a integer
        jsonObject.put("people", people);
        jsonObject.put("date",reserveDate);
        jsonObject.put("time", reserveTime);
        jsonArray.put(jsonObject);

        Log.i("jsonString", jsonObject.toString());


    }catch(Exception e){

    }


    JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST,domain + api, jsonObject,
                new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(String response) {

                    Log.e("Response", response);

                    return;
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    Log.e("Error.Response", error.toString());
                    String json = null;

                    NetworkResponse response = error.networkResponse;
                    if(response != null && response.data != null){
                        switch(response.statusCode){
                            case 400:
                                json = new String(response.data);
                                System.out.println(json);
                                //json = trimMessage(json, "message");
                                //if(json != null) displayMessage(json);
                                break;
                        }
                        //Additional cases
                    }

                }

            })

    {
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put("store",storeId);
            params.put("people",String.valueOf(people));
            params.put("date", "2017-01-04");
            params.put("time", reserveTime);
            Log.d("params", params.toString());
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("Authorization", finalToken);
            Log.d("headers", headers.toString());
            return headers;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • Thanks for your reply, however I am still getting the error that people is a string type , need integer – JerryKo Jan 04 '17 at 01:05
0

Very simple, when parameter data type is integer, then convert the integer to string like this

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            int number = 999;
            Map<String, String> params = new HashMap<>();
            params.put("key", Integer.toString(number));
            return params;
        }