如何使用reftend android发布raw gson以及如何为此创建模型类

oknwwptz  于 2022-11-06  发布在  Android
关注(0)|答案(2)|浏览(125)
{
    "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
        }
    ]
}

这是我的 Postman 原始gson对象,现在我想如何从改造Android调用

l7mqbcuq

l7mqbcuq1#

创建一个类以接受Cart数组

public class ResponseObject {

 public Data[] cart;

}

然后创建一个Data类来获取数组中的字段,并创建它的getter方法。

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;
    }
}

创建一个Api类来存储服务,如下所示:

public interface AllApi {

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

然后接受来自服务器的数据,使用类似于

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) {

            }
        });
clj7thdc

clj7thdc2#

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();
     }

相关问题