Volley parrams,如何发送值 95 以便服务器不会收到“95”,但会收到 95

Volley parrams, how to send the value 95 so the server won't receive "95" but it will receive 95

好的,问题是我在 Postman 中测试过

{
    "country": "Australia",
    "windProbability": "90"
}

不会给我结果 但是

{
    "country": "Australia",
    "windProbability": 90
}

工作正常

问题是当我发送

protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            String country = getIntent().getStringExtra("country");
            String windProbability = getIntent().getStringExtra("windProbability");
            if(country==null){
                country="";
                windProbability="";
            }
            params.put("country", country);
            params.put("windProbability", String.valueOf(windProbability));
            return params;
        }

服务器收到

{
    "country": "Australia",
    "windProbability": "90"
}

使用 JSONObject 对象而不是 Map 添加您的请求参数。

void sendTestRequest() throws JSONException {
    RequestQueue queue = Volley.newRequestQueue(this);
    String url = "http://www.google.com";
    JSONObject jsonRequest = new JSONObject();
    jsonRequest.put("country", "Australia");
    jsonRequest.put("windProbability", 90);
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, jsonRequest,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

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

删除获取参数方法并像这样发送。您不能使用 getparams 函数发送 int 值。

        String url = "your url";
        JSONObject postparams = new JSONObject();
        try {
            postparams.put("country", "Australia");
            postparams.put("windProbability", 90); 
        // make it dynamic by declaring windProbalility as int
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Log.d(TAG,postparams.toString());

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                url, postparams,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {    

                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {


                Log.e(TAG+"Error: ",error.getMessage());
            }
        });