I'm trying to send data to a WepApi service, specifically to a POST method.
The method I use to send the data is:
private void enviaDatos()
{
EditText txtAsunto = (EditText) viewPrincipal.findViewById(R.id.asunto);
// Store values at the time of the login attempt.
String asunto= txtAsunto.getText().toString();
EditText txtDescripcion = (EditText) viewPrincipal.findViewById(R.id.descripcion);
// Store values at the time of the login attempt.
String descripcion= txtDescripcion.getText().toString();
String idArea =((Area) sAreas.getSelectedItem()).getId();
String idTipoComunicacion = ((TipoComunicacion) sTipos.getSelectedItem()).getID();
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("idArea", idArea );
jsonObject.put("idTipoComunicacion", idTipoComunicacion);
jsonObject.put("idCiudadano", Ciudadano.getId());
jsonObject.put("idEstado", "1");
jsonObject.put("asunto", asunto);
jsonObject.put("descripcion", descripcion);
}
catch (JSONException e)
{
e.printStackTrace();
}
String JsonURL = getString(R.string.servidor)+"/api/comunicaciones";
JsonObjectRequest postRequest = new JsonObjectRequest(
Request.Method.POST,
JsonURL,
jsonObject,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
try {
showProgress(false);
Log.e("VOLLEY","OK");
} catch (Exception e) {
Log.e("VOLLEY", e.getMessage());
showProgress(false);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", "ERROR LISTENER:"+error.getMessage());
showProgress(false);
}
}
)
{
@Override
public Map getHeaders() throws AuthFailureError {
Map headers = new HashMap<>();
String credentials = Ciudadano.getEmail()+":"+Ciudadano.getPalabra();
String auth = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
headers.put("Content-Type", "application/json");
headers.put("Authorization", auth);
return headers;
}
};
// Adds the JSON array request "arrayreq" to the request queue
requestQueue.add(postRequest);
}
The method in the WebApi is simply add a record in the database with the same fields:
// POST: api/Comunicaciones
[BasicAuthenticationFilter]
[ResponseType(typeof(Comunicacion))]
public IHttpActionResult PostComunicacion(Comunicacion comunicacion)
{
comunicacion.Id = Guid.NewGuid().ToString();
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Comunicaciones.Add(comunicacion);
try
{
db.SaveChanges();
}
catch (DbUpdateException)
{
if (ComunicacionExists(comunicacion.Id))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = comunicacion.Id }, comunicacion);
}
and when I execute it I get the error:
E/Volley: [388] BasicNetwork.performRequest: Unexpected response code 400 for http://192.168.10.199/ws/api/comunicaciones
E / VOLLEY: ERROR LISTENER: null
If I comment on the getHeaders method and the WebApi method, I mention [BasicAuthenticationFilter], which is where I check the user and password, everything works great, saves the record in the database, etc., as it should be.
If I mention the getHeaders method and leave [BasicAuthenticationFilter] without comment, I get the 401 code that is normal, without authorization,
But if I leave the two without comment, or enter the webapi, or checking the username and password, or anything.
The webapi I tried it from another application and it works correctly and the same code of the getheader method I use it in the same screen to bring data and it also works well
Someone can help me out.
Thanks