0
I’m getting error 400 when sending parameters to the web service.
@POST
@Produces("application/json; charset=utf-8")
@Consumes("application/json; charset=utf-8")
@Path("realizarvenda")
public String realizarVenda(HashMap<String, String> json) {
VendaDAO vendDAO = new VendaDAO();
Venda venda = new Venda();
ClienteDAO cliDAO = new ClienteDAO();
EmpresaDAO empresaDAO = new EmpresaDAO();
System.out.println(json.toString());
venda.setCliente(cliDAO.carregar(Long.parseLong(json.get("idCliente"))));
venda.setEmpresa(empresaDAO.carregar(Long.parseLong(json.get("idEmpresa"))));
venda.setStatusVenda(json.get("statusVenda"));
venda.setFormaPagtoVenda(json.get("formaPagamento"));
Date data = new Date();
venda.setDataVenda(data);
Integer tamanho = Integer.parseInt(json.get("tamanho"));
List<VendaProduto> carrinhoCompra = new ArrayList<>();
for(int i = 0;i<tamanho;i++){
ProdutoDAO prodDAO = new ProdutoDAO();
VendaProduto item = new VendaProduto();
item.setProduto(prodDAO.carregar(Long.parseLong(json.get("item"+i+"produto"))));
item.setQuantVendaProduto(Integer.parseInt(json.get("item"+i+"quantidade")));
item.setVlrVendaProduto(Double.parseDouble(json.get("item"+i+"valor")));
item.setVenda(venda);
carrinhoCompra.add(item);
}
venda.setVendaProduto(carrinhoCompra);
Venda vendaretorno = vendDAO.cadastrarMobile(venda);
if(vendaretorno.getIdVenda() != null)
return "Sucesso";
else
return "Erro";
}
And on android:
public static void enviarVenda() {
url = "http://192.168.1.107:8080/BMSystem/webresources/venda/realizarvenda/";
params = new HashMap<String, String>();
params.put("idCliente", cli.getIdPessoa().toString());
params.put("idEmpresa", cli.getEmpresa().getIdEmpresa().toString());
params.put("formaPagamento", vend.getFormaPagtoVenda());
params.put("vlrVenda", vend.getVlrVenda().toString());
params.put("statusVenda", vend.getStatusVenda());
params.put("tamanho", ""+vend.getVendaProduto().size());
for(int i = 0; i<vend.getVendaProduto().size();i++){
params.put("item"+i+"produto", vend.getVendaProduto().get(i).getProduto().getIdProduto().toString());
params.put("item"+i+"quantidade", vend.getVendaProduto().get(i).getQuantVendaProduto().toString());
params.put("item"+i+"valor", vend.getVendaProduto().get(i).getVlrVendaProduto().toString());
}
Log.i("Parametros",params.toString());
StringRequest request = new StringRequest(Method.POST,
url,
new Response.Listener<String>(){
@Override
public void onResponse(String response) {
Log.i("Script", "SUCCESS: "+response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("Script", "Erro: "+error);
}
}){
@Override
public Map<String, String> getParams() throws AuthFailureError{
return(params);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
@Override
public Priority getPriority(){
return(Priority.NORMAL);
}
};
rq.add(request);
}
Error received:
04-29 07:51:34.303: E/Volley(2261): [128] BasicNetwork.performRequest: Unexpected response code 400 for http://192.168.1.107:8080/BMSystem/webresources/venda/realizarvenda/
04-29 07:51:34.303: I/Script(2261): Erro: com.android.volley.ServerError
If your request is made in the format JSON, why not use
JsonObjectRequest
? Error 400 is invalid request, I believe it has something to do with the format being done.– Paulo Rodrigues
i was using Jsonbbject but it wasn’t working either. But as my answer is only string I did with Stringrequest
– Bruno Nicoletti
I believe that the error is on the web service only
– Bruno Nicoletti
Therefore, the header of the request should not be
application/json
, for this case the request expects something in the format JSON on the body.– Paulo Rodrigues
right, how would the header then?
– Bruno Nicoletti
This is the standard for sending a request POST, utilize
application/x-www-form-urlencoded
. Behold here an example of simple request by POST.– Paulo Rodrigues
presented the same error. and by what I checked in the example you passed is almost the same. It would not be an error in the web service?
– Bruno Nicoletti
The upload I’m doing on android is correct, but what I’m seeing I’m not getting the values through the correct POST method? if yes, how would I do this?
– Bruno Nicoletti