0

I want to set application/json as the Content type but even after overriding getBodyContentType() to application/json getting Volley server error.

  StringRequest postRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        listener.onResponse(response);
                        Log.d("Response",response.toString());

                    } catch (Exception e) {
                        e.printStackTrace();
                        if (response.length() == 0) {
                            Log.d("ERROR : ", "NO RESPONSE");
                        } else {
                            Log.d("ERROR : ", "EXCEPTION");
                        }
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error
                    listener.onError(error.toString());
                    Log.d("Error.Response", error.toString());
                }
            }
    ) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
                params.put("macId", macValue);
            return params;
        }
      @Override
        public String getBodyContentType() {
            return "application/json";
        }

    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(postRequest);
}

Note : please provide the answers in code snippet

Abiranjan
  • 507
  • 4
  • 18

2 Answers2

0

override following method:

  public String getBodyContentType() {
        return "application/json; charset=utf-8";
    }

and override getHeaders as well and pass nothing:

   @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        return headers;
    }

it will work.

Anjali
  • 1
  • 1
  • 13
  • 20
0

First check your webservice in postman...

SHA
  • 57
  • 4