繁体   English   中英

如何设置身体以获得凌空请求?

[英]How to set body to get volley request?

美好的一天,我在 Android Volley 中将正文设置为Request.Method.GET时遇到问题,我的代码如下所示:

public void getRideItem(String userToken) {

    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

    String sendRequest;
    JSONObject requestBody = new JSONObject();
    try {
        requestBody.put("lat", "-64.9631");
        requestBody.put("lng", "40.719296");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    sendRequest = requestBody.toString();

    String mURL = "https://taxiapp.webfumeprojects.online/api/ride/getHome";

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, mURL,
            null,
            response -> {
                Log.i(TAG, "Mahdi: HomeFragment: getCar: res 0 " + response);
            }, error -> {
        error.printStackTrace();
    }) {
        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }

        @Override
        public byte[] getBody() {

            byte[] body = new byte[0];
            try {
                body = sendRequest.getBytes("UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.fillInStackTrace());
            }
            return body;
        }

        @Override
        public Map<String, String> getHeaders() {
            Map<String, String> params = new HashMap<>();
            params.put("token", userToken);
            return params;
        }

        @Override
        protected Response parseNetworkResponse(NetworkResponse response) {
            try {
                Log.i(TAG, "Mahdi: HomeFragment: getCar: res 1 " + response.data);
                String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
                return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response));
            } catch (UnsupportedEncodingException | JSONException e) {
                return Response.error(new ParseError(e));
            }
        }
    };
    requestQueue.add(jsonObjectRequest);

}

但是当我运行我的应用程序时,我从 Volley com.android.volley.TimeoutError得到这个错误,以及来自 postman 的一些图像以获取更多信息:

授权标签:

在此处输入图像描述

正文选项卡:

在此处输入图像描述

所以我首先要做的是测试 Json Volley 请求正在生成jsonObject.toString(); 然后亲自为我的 Volley 请求我的身体请求如下所示

  @Override
    public byte[] getBody()
    {
        byte[] body;
        body = mRequestBody.getBytes(StandardCharsets.UTF_8);
        return body;
    }

暂无
暂无

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

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