Search code examples
javaandroidandroid-volleygetmethod

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 server error in android studio and I entered error response.here is my codes: ant solution???? please

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

Solution

  • Please show the error message you have. Also I am not sure how ok it is to send something in a body of a GET request. This answer might help you:

    HTTP GET with request body