Request with Volley Webservice does not recognize json

2

I am trying to do a POST request with Android Volley, but my Webservice does not recognize the json file that is sent as a parameter. With the Google Chrome Postman plugin Webservice consumes perfectly without any problems.

Below is my method that consumes json in Webservice:

@POST
@Path("/login")
@Consumes("application/json")
@Produces("application/json")
public Response authenticate(Usuario usuario) {

    try{

        System.out.println("CPF: " + usuario.getCpfUsuario());

        return Response.ok("OK").build();

    } catch (Exception e) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
    }    

}

My Android client:

Usuario u = new Usuario("sadsadsad","asdaddad");
Gson gson = new Gson();

String s =  new String(gson.toJson(u));
Map<String, String> params  = new HashMap<String, String>();
params.put("Usuario", s);

JsonObjectRequest mCustomRequest = new JsonObjectRequest(
            Request.Method.POST,
            "http://(meuip):8080/webservice/authentication/login",
            new JSONObject(params),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    mTextView.setText("Retorno: " + response);
                }
            },
            new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError error) {
                    mTextView.setText("Falhou: " + error);
                    Log.i("SAIDA: ", "" + error);
                }
            }){

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Accept", "application/json; charset=utf-8");
            return headers;
        }

    };

    NetworkConnection.getInstance(getActivity()).addRequestQueue(mCustomRequest);

The output in Webservice when accessed with the Android client is CPF: null and with Postman CPF: "passed value"

The response of the android client is: com.android.volley.AuthFailureError

    
asked by anonymous 22.09.2015 / 14:54

1 answer

1

In the example above, it was only necessary to comment the lines:

Usuario u = new Usuario("sadsadsad","asdaddad");
Gson gson = new Gson();

String s =  new String(gson.toJson(u));

Then pass the parameters correctly:

Map<String, String> params  = new HashMap<String, String>();
params.put("cpfUsuario", "11111111111");
params.put("senhaUsuario", "blablabla");
    
22.09.2015 / 19:15