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