【问题标题】:Volley parrams, how to send the value 95 so the server won't receive "95" but it will receive 95Volley parrams,如何发送值 95,以便服务器不会收到“95”但会收到 95
【发布时间】:2019-08-10 17:30:06
【问题描述】:

好的,问题是我在 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"
}

【问题讨论】:

  • 我认为没有必要 .. Body 将作为 String 你应该修改你的服务器逻辑来解析请求。
  • @ADM 问题是我为实习创建了这个应用程序,所以我无法修改服务器逻辑,因为它不在我身边:|最糟糕的是我快完成了..我不知道如何解决这个问题..
  • 您可以尝试使用Json请求stackoverflow.com/a/26347450/7995966。我很久没用 Volley 了..
  • @user3377063,您将String 作为windProbability String.valueOf(windProbability)); 发送,服务器响应将作为String 响应

标签: android api parameters android-volley endpoint


【解决方案1】:

删除 get params 方法并像这样发送。您不能使用 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());
            }
        });

【讨论】:

  • 非常感谢,问题解决了。我很高兴 ATM :)
  • ATM ??。我是贾林。很高兴您的问题得到解决。
【解决方案2】:

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

【讨论】:

  • Map&lt;String, String&gt; 不会接受 Integer
  • 你能把你指定凌空请求的sn-p发给我吗?
  • 我怎么寄给你? @noahutz
  • 你能试试新的解决方案吗?
  • 我正在尝试了解如何使用您的解决方案更改我的代码以进行测试。
猜你喜欢
  • 2016-12-29
  • 1970-01-01
  • 1970-01-01
  • 2011-08-25
  • 2011-07-16
  • 2020-09-10
  • 1970-01-01
  • 2019-06-26
  • 1970-01-01
相关资源
最近更新 更多