简体   繁体   English

使用有效载荷 Volley 发送 GET 请求

[英]Sending a GET request with payload Volley

I want to make a GET request using Volley in Android Studio and in this request I want to include a body.我想在 Android Studio 中使用 Volley 发出一个 GET 请求,在这个请求中我想包含一个主体。 The problem is that when using the GET request, the body in the server is None (I am using Flask).问题是在使用GET请求时,服务器中的body为None (我使用的是Flask)。 On the other side, when using the POST method, the body reaches the server correctly.另一方面,当使用 POST 方法时,正文正确到达服务器。 This is my code:这是我的代码:

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

Is this a Volley restriction, or am I doing something wrong?这是凌空限制,还是我做错了什么? Assume that I am using a JSONObject as body, non empty.假设我使用JSONObject作为主体,非空。

please check this answer请检查这个答案
HTTP GET with request body 带有请求正文的 HTTP GET

and Use POST Http method to send Body!!并使用POST Http方法发送Body!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM