1

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);
}
Amin Pinjari
  • 1,693
  • 2
  • 19
  • 48
zahra7329
  • 33
  • 5

2 Answers2

1

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

Yavor Mitev
  • 410
  • 3
  • 12
  • You can see here that you can be more specific. https://stackoverflow.com/questions/39893772/in-volley-library-giving-com-android-volley-servererror – Yavor Mitev Oct 03 '19 at 07:47
0

It's better to use post method for this type of request. But still if you want to use GET method then you should have to pass token in the URL. Below is an example

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;