检查通过改造发送的json

时间:2016-04-14 13:48:20

标签: android json post retrofit

我想在我的入队通话中检查通过改造发送的原始json。这是我点击事件的代码:

            public void onClick(View v) {
                ArrayList<Product> products = ((MenuAdapter)rvMenuProducts.getAdapter()).getOrderItens();
                ArrayList<Item> orderItemList = new ArrayList<Item>();
                for(Product p: products){
                    orderItemList.add(new Item(p));
                }
                EditText etCardNumber = (EditText) findViewById(R.id.et_card_number);
                String cardNumber = etCardNumber.getText().toString();

                if(GenericUtils.stringIsNotNullAndNotEmpty(cardNumber)){
                    NewOrder newOrder = new NewOrder(waiterId, establishmentId, cardNumber, orderItemList);

                    WaiterRequester.addOrder(newOrder, new Callback<AddOrderResultInfo>() {
                        @Override
                        public void onResponse(Call<AddOrderResultInfo> call, Response<AddOrderResultInfo> response) {
                            int a = 5;

                            Toast.makeText(context, "Pedido realizado com sucesso", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onFailure(Call<AddOrderResultInfo> call, Throwable t) {
                            int b = 5;

                            Toast.makeText(context, "VISH", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
                else{
                    etCardNumber.setError("ERROOOOOOO");
                }
            }

这就是我如何调用我的API,并且我在我的api上得到一些例外,我不知道,因为我的返回是一个布尔值,但是当我发送请求时使用邮差,它有效!

这是邮递员发送的json:

{   
    "newOrder": 
    {
        "idWaiter" : 1,
        "idEstablishment": 6,
        "cardNumber": "123",
        "listItem": [{ "idProduct": 1, "quantity": 1 }]
    }
}

我的班级NewOrder在哪里:

public class NewOrder {

    public int idWaiter;
    public long idEstablishment;
    public String cardNumber;
    public ArrayList<Item> listItem;

    public NewOrder() {
    }

    public NewOrder(int idWaiter, long idEstablishment, String cardNumber, ArrayList<Item> listItem) {
        this.idWaiter = idWaiter;
        this.idEstablishment = idEstablishment;
        this.cardNumber = cardNumber;
        this.listItem = listItem;
    }

    public int getIdWaiter() {
        return idWaiter;
    }

    public void setIdWaiter(int idWaiter) {
        this.idWaiter = idWaiter;
    }

    public long getIdEstablishment() {
        return idEstablishment;
    }

    public void setIdEstablishment(long idEstablishment) {
        this.idEstablishment = idEstablishment;
    }

    public String getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }

    public ArrayList<Item> getListItem() {
        return listItem;
    }

    public void setListItem(ArrayList<Item> listItem) {
        this.listItem = listItem;
    }
}

和项目:

public class Item {

    public long idProduct;
    public int quantity;

    public Item() {
    }

    public Item(Product p) {
        this.setQuantity(p.getQuantity());
        this.setIdProduct(p.getIdProduct());
    }

    public Item(int idProduct, int quantity) {
        this.idProduct = idProduct;
        this.quantity = quantity;
    }

    public long getIdProduct() {
        return idProduct;
    }

    public void setIdProduct(long idProduct) {
        this.idProduct = idProduct;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}

编辑:

它可能很有用,By Object在后端以null形式到达。

1 个答案:

答案 0 :(得分:2)

在我看来,您正在使用Retrofit 2,因此,您可以制作一个拦截器来记录您的所有请求。

'com.squareup.okhttp3:logging-interceptor:3.2.0'添加到您的依赖项中,然后像这样构建OkHttpClient

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

最后,将您的OkHttpClient添加到Retrofit对象:

retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .build();