Volley POST整数

时间:2017-01-03 08:54:59

标签: java android android-volley

我正在发出一个截击请求并试图发布一个整数,按照此解决方案Send Int in HashMap with Volley我的最终代码如下所示:

        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);
}

这里我想将“people”作为整数发布,但我得到的错误信息是“people”是一个字符串值,我已经将“people”声明为:

 Integer people = Integer.parseInt(reserveNum);

其中reserveNum是edittext的数字值。如何在我的请求中将“peopel”变成整数?

UPDATE 错误信息是:

{"errors":[{"message":"Invalid type: string (expected integer)","params":{"type":"string","expected":"integer"},"code":0,"dataPath":"/people","schemaPath":"/properties/people/type",.....

3 个答案:

答案 0 :(得分:1)

我通过将Stringrequest更改为JSONObject reqeust来找到解决方案。通过这样做,我的请求将采用包含整数而不是字符串的JSON格式。

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);
}

答案 1 :(得分:0)

尝试以下代码替换上面的代码:

    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);
}

答案 2 :(得分:0)

非常简单,当参数数据类型为整数时,将整数转换为字符串

        @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;
        }