Error 400 android for web service

Asked

Viewed 216 times

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.

  • i was using Jsonbbject but it wasn’t working either. But as my answer is only string I did with Stringrequest

  • I believe that the error is on the web service only

  • 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.

  • right, how would the header then?

  • This is the standard for sending a request POST, utilize application/x-www-form-urlencoded. Behold here an example of simple request by POST.

  • 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?

  • 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?

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.