0

This is my cart items data getting into gson object

{"cart":[{"id":"6100","productId":"47","UserRegistrationId":"44","TotalQuantity":1,"Status":"CheckOut","Price":200},{"id":"6104","productId":"154","UserRegistrationId":"44","TotalQuantity":1,"Status":"CheckOut","Price":100}]}

I'm getting error and I can't post the data to the server

D/OkHttp: {"nameValuePairs":{"cart":{"values":[{"nameValuePairs":{"id":"6100","productId":"47","UserRegistrationId":"44","TotalQuantity":1,"Status":"CheckOut","Price":200}},{"nameValuePairs":{"id":"6104","productId":"154","UserRegistrationId":"44","TotalQuantity":1,"Status":"CheckOut","Price":100}}]}}}

This is my code

String status = "CheckOut";

                try {

                    JSONArray cart = new JSONArray();
                    JSONObject jsonObject1 = new JSONObject();
                    for (GetCartDataModel getCartDataModel : getCartDataModelList) {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("id", getCartDataModel.getId());
                        jsonObject.put("productId", getCartDataModel.getProductId());
                        jsonObject.put("UserRegistrationId", getCartDataModel.getUserRegistrationId());
                        jsonObject.put("TotalQuantity", getCartDataModel.getTotalQuantity());
                        jsonObject.put("Status", status);
                        jsonObject.put("Price", getCartDataModel.getPrice());
                        cart.put(jsonObject);
                    }
                    jsonObject1.put("cart", cart);
                    Log.e("cart", String.valueOf(jsonObject1));
                    Toast.makeText(mContext, String.valueOf(jsonObject1), Toast.LENGTH_SHORT).show();

                    Call<CheckOutDataModel> call = RetrofitClient.getInstance().getApi().sendArray(jsonObject1);
                    call.enqueue(new Callback<CheckOutDataModel>() {
                        @Override
                        public void onResponse(Call<CheckOutDataModel> call, Response<CheckOutDataModel> response) {
                            CheckOutDataModel checkOutDataModel = response.body();
                            Toast.makeText(mContext, checkOutDataModel.getMessage(), Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onFailure(Call<CheckOutDataModel> call, Throwable t) {

                        }
                    });

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

Interface Class

 @POST("CheckOut")
    Call<CheckOutDataModel> sendArray(@Body JSONObject jsonObject1);

Model Class

package com.nextsuntech.kdf1.Model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import org.json.JSONArray;

import java.util.List;

public class CheckOutDataModel {

    @SerializedName("cart")
    @Expose
    private List<CheckOutDataModel> cart = null;

    public List<CheckOutDataModel> getCart() {
        return cart;
    }

    public void setCart(List<CheckOutDataModel> cart) {
        this.cart = cart;
    }


    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("ProductId")
    @Expose
    private Integer productId;
    @SerializedName("UserRegistrationId")
    @Expose
    private Integer userRegistrationId;
    @SerializedName("TotalQuantity")
    @Expose
    private Integer totalQuantity;
    @SerializedName("Status")
    @Expose
    private String status;
    @SerializedName("Price")
    @Expose
    private Integer price;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getProductId() {
        return productId;
    }

    public void setProductId(Integer productId) {
        this.productId = productId;
    }

    public Integer getUserRegistrationId() {
        return userRegistrationId;
    }

    public void setUserRegistrationId(Integer userRegistrationId) {
        this.userRegistrationId = userRegistrationId;
    }

    public Integer getTotalQuantity() {
        return totalQuantity;
    }

    public void setTotalQuantity(Integer totalQuantity) {
        this.totalQuantity = totalQuantity;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

Response Class

 @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("autoId")
    @Expose
    private Integer autoId;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getAutoId() {
        return autoId;
    }

    public void setAutoId(Integer autoId) {
        this.autoId = autoId;
    }

Postman API

James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

0

Don't use JSONObject, use request POJO

  1. Create POJO class using https://www.jsonschema2pojo.org/ for your json request

  2. create request using POJO class getter and setters

  3. Use POJO class object in retrofit.

     Example: 
    
     class RequestCart() {
     //..getter//..setter
     }
    

    use it following way

     @POST("checkout")
     Call<CheckOutDataModel> sendArray(@Body request:  RequestCart);
    
  • The question itself features a far better example than this, even if posting eg. class `RequestCart` is correct. But unless using nested model classes, this will not work out ... – Martin Zeitler Feb 16 '22 at 19:41
  • Sorry to say but I can't understand – Faizan Ul Hassan Feb 17 '22 at 12:21
  • Do you understand POJO? If yes, you have to create one for your json request. Create request object of type POJO and pass it as @Body to retrofit. If you do not understand POJO, have a look on the reference link given in solution, it has nice examples – Gaurav Chaudhari Feb 17 '22 at 15:27