发送带有有效载荷Volley的GET请求

问题描述 投票:0回答:1

我想在Android Studio中使用Volley发出GET请求,并且在此请求中我希望包含主体。问题是,当使用GET请求时,服务器中的正文为None(我在使用Flask)。另一方面,使用POST方法时,主体可以正确到达服务器。这是我的代码:

        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        String url = Globals.WINGS_VEHICLES_URL;

        JSONObject payload = new JSONObject();
        try {
            payload.put("user_id", Globals.USER_ID);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        final String requestBody = payload.toString();
        System.out.println(requestBody);

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                System.out.println(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }){
            @Override
            public String getBodyContentType() {
                return "application/json";
            }

            @Override
            public byte[] getBody() {
                return requestBody.getBytes();
            }


        };

        stringRequest.setRetryPolicy(new DefaultRetryPolicy(0, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(stringRequest);

这是排球限制,还是我做错了什么?假设我使用JSONObject作为正文,非空。

java android android-volley
1个回答
0
投票

请检查此答案HTTP GET with request body

并使用POST Http方法发送正文!

© www.soinside.com 2019 - 2024. All rights reserved.