如何使用Volley在Android中将请求主体数据发送到GET方法?

3

我有一个API,当我向服务器发送令牌时,它会向我发送一个JSON对象,我使用GET方法并且必须将令牌作为请求体发送,而不是标头。当我把令牌放在请求体中时,Postman可以正常工作,但是在Android Studio中使用volley时会出现服务器错误,我已经查看了错误响应。这是我的代码:有任何解决方案吗?

   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);
}
2个回答

1
请提供您所遇到的错误信息。同时,我不确定在GET请求的body中发送内容是否可行。这个答案可能会对您有帮助:HTTP GET with request body

你可以在这里看到,你可以更加具体。https://dev59.com/Jpvga4cB1Zd3GeqP1GTY - Yavor Mitev

0

对于这种类型的请求,最好使用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;

https://demonuts.com/android-volley-get-request-with-parameters/ - Abdullah Javed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接