-2
{
    "cart":[
        {
        "id": 6083,"ProductId": 27,"UserRegistrationId": 44,"TotalQuantity": 2,"Status":"CheckOut","Price": 1800
        },
        {
        "id": 6085,"ProductId": 26, "UserRegistrationId":44, "TotalQuantity":2, "Status":"CheckOut", "Price":1800
        }
    ]
}

This my postman raw gson object now i want how call from retrofit android

2 Answers2

1

Create a class to accept the Cart array

public class ResponseObject {

 public Data[] cart;

}

Then create a class Data to get the fields inside the array and create its getter methods.

public class Data{

    public String id;
    public String ProductId;
    public String UserRegistrationId;

 public String getId() {
        return id;
    }

    public String getProductId() {
        return ProductId;
    }

    public String getUserRegistrationId() {
        return UserRegistrationId;
    }
}

Create a Api class to store your services in as follows:


public interface AllApi {

    @FormUrlEncoded
    @POST("<your php / api url here>")
    Call<ReturnedResponsePojo> getCartItems(@Field("Order_Id") String Order_Id);
}

Then Accept the data from server using retrofit like

    Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.APP_SERVER_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        AllApi mApiService = retrofit.create(AllApi.class);
        Call<ResponseObject> mService = mApiService.getCartItems("<Order ID here>");
        mService.enqueue(new Callback<ResponseObject>() {
            @Override
            public void onResponse(Call<ResponseObject> call, Response<ResponseObject> response) {

                ResponseObject jsonResponse = response.body();

                ArrayList<Data> dataArray = new ArrayList<>(Arrays.asList(jsonResponse.getCart()));
                
                *** Write your required code here ***
                

            }

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

            }
        });
Sahil
  • 69
  • 8
0
 String Status = "CheckOut";
     try {
         JSONArray cart = new JSONArray();
         for (GetCartDataModel getCartDataModel : getCartDataModelList) {
             JSONObject jsonObject = new JSONObject();
             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);    

             Log.e("Cart", String.valueOf(cart));

             Call<CheckOutResponse> call = RetrofitClient.getInstance().getApi().sendCart();
             call.enqueue(new Callback<CheckOutResponse>() {
                 @Override
                 public void onResponse(Call<CheckOutResponse> call, Response<CheckOutResponse> response) {
                     CheckOutResponse checkOutResponse = response.body();
                     if (response.isSuccessful()) {
                         Toast.makeText(mContext, checkOutResponse.getMessage(), Toast.LENGTH_SHORT).show();
                     }
                 }

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

                 }
             });
         }

     }catch(JSONException e){
         e.printStackTrace();
     }
Vladimir Salguero
  • 5,609
  • 3
  • 42
  • 47