繁体   English   中英

如何使用 volley 将 Body Data 发送到 GET Method Request android?

[英]How to send Body Data to GET Method Request android using volley?

I have an API which send me a JSON object when I send a token to the server, I use GET method and I have to send token in body, not headers, it works in postman correctly when I put token in body but I have volley android 工作室中的服务器错误,我输入了错误响应。这是我的代码:ant 解决方案????

   private void getFreightsFromServer()
{
    final String url =" https://parastoo.app/api/driver-cargo-list";

    JSONObject jsonData = new JSONObject();

    String token = G.getString("token");
    try
    {
        jsonData.put("token", token);


    } catch (JSONException e)
    {
        e.printStackTrace();
    }


    Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>()
    {
        boolean isGet = false;

        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public void onResponse(JSONObject response)
        {
            try
            {
                    MyPost post = new MyPost();

                    JSONArray jsonArray = response.getJSONArray("results");

                    for (int i = 0; i < jsonArray.length(); i++)
                    {
                        JSONObject tempJsonObject = jsonArray.getJSONObject(i);
                        JSONObject jsonOriginCustomer = new JSONObject(tempJsonObject.getString("origin_customer"));
                        post.setOriginCity(jsonOriginCustomer.getString("customerCity"));
                        JSONObject jsonDestinationCustomer = new JSONObject(tempJsonObject.getString("destination_customer"));
                        Log.d("result", jsonDestinationCustomer.getString("customerCity"));
                        post.setDestinationCity(jsonDestinationCustomer.getString("customerCity"));
                        freightsList.add(post);
                      //  isGet = true;
                        adapter.notifyDataSetChanged();
                    }

            } catch (JSONException e)
            {
                e.printStackTrace();
            }


        }

    };

    Response.ErrorListener errorListener = new Response.ErrorListener()
    {
        @Override
        public void onErrorResponse(VolleyError error)
        {

            Toast.makeText(getContext(), error.toString(), Toast.LENGTH_LONG).show();
            Log.d("jdbvdc", error.toString());
            error.printStackTrace();
        }
    };

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, jsonData, listener, errorListener);
    Log.d("fhdhdcf",jsonData.toString());


    final int socketTimeout = 100000;
    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    request.setRetryPolicy(policy);

    AppSingleton.getInstance(getContext()).addToRequestQueue(request);
}

请显示您的错误消息。 此外,我不确定在 GET 请求的正文中发送一些东西有多好。 这个答案可能会帮助你:

HTTP GET 请求正文

对于这种类型的请求,最好使用 post 方法。 但是如果你想使用 GET 方法,那么你应该在 URL 中传递令牌。 下面是一个例子

final String username = etUname.getText().toString().trim();
final String password = etPass.getText().toString().trim();

URLline = "https://demonuts.com/Demonuts/JsonTest/Tennis/loginGETrequest.php?username="+username+"&password="+password;

暂无
暂无

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

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