如何在改造时使用数组传递 json 对象
How to pass json object with array on retrofit
当我通过邮递员发送请求作为行请求时(Json)我得到了成功响应,但是从我的 android 设备它没有成功。
邮递员请求
这是我的 Android 代码,
界面ApiService.Class
@Headers("Content-Type: application/json")
@POST("api/ShopData/AddProduct")
Call<AddNewProductResponse> addNewProduct(@Header("Authorization") String authorization, @Body JsonObject jsonObject);
Class ApiClient.Class
public static Call<AddNewProductResponse> addNewProduct(String token, JsonObject jsonObject) {
token = "Bearer " + token;
Call<AddNewProductResponse> call = apiService.addNewProduct(token, jsonObject);
return call;
}
API 通话
private void addNewProductToServer(JsonObject jsonObject) {
String token = AppSettings.getInstance(getActivity()).getStringValue(PrefKeys.token);
if (token != null && jsonObject != null) {
ApiClient.addNewProduct(token, jsonObject).enqueue(new Callback<AddNewProductResponse>() {
@Override
public void onResponse(Call<AddNewProductResponse> call, retrofit2.Response<AddNewProductResponse> response) {
if (response != null && response.body() != null) {
if (response.body().getMessage().getCode() == 1) {
showProductAddedSuccessDialog();
} else
Helper.showAlertDialogOK(getActivity(), Helper.getErrorMessages(response.body().getMessage().getCode()));
} else
Helper.showServerErrorDialog(getActivity());
}
@Override
public void onFailure(Call<AddNewProductResponse> call, Throwable t) {
Helper.showServerErrorDialog(getActivity());
}
});
}
}
创建请求Json对象,
JsonObject jsonObject = new JsonObject();
JsonArray images = new JsonArray();
try {
if (productImage != null) {
JsonObject imgObject = new JsonObject();
imgObject.addProperty("attachment", productImage.getAttachment());
imgObject.addProperty("position", 1);
images.add(imgObject);
}
jsonObject.addProperty("name", edtTxtProductName.getText().toString().trim());
jsonObject.addProperty("short_description", edtTxtShortDescription.getText().toString().trim());
jsonObject.addProperty("full_description", edtTxtLongDescription.getText().toString().trim());
jsonObject.addProperty("sku", edtTxtSku.getText().toString().trim());
jsonObject.addProperty("stock_quantity", Integer.parseInt(edtTxtStockQuantity.getText().toString().trim()));
jsonObject.addProperty("price", Float.parseFloat(edtTxtPrice.getText().toString().trim()));
jsonObject.addProperty("images", String.valueOf(images));
addNewProductToServer(jsonObject);
} catch (Exception e) {
e.printStackTrace();
}
您需要替换行
jsonObject.addProperty("images", String.valueOf(images));
来自
jsonObject.add("images", images);
创建您的模型类并设置您的值
public class ModelClass {
@SerializedName("name")
private String name = "";
@SerializedName("short_description")
private String shortDesc = "";
@SerializedName("full_description")
private String fullDescription = "";
@SerializedName("sku")
private String sku = "";
@SerializedName("stock_quantity")
private int qty = 0;
@SerializedName("price")
private double price = 0.0;
@SerializedName("images")
private ArrayList<ImageModel> imageList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getShortDesc() {
return shortDesc;
}
public void setShortDesc(String shortDesc) {
this.shortDesc = shortDesc;
}
public String getFullDescription() {
return fullDescription;
}
public void setFullDescription(String fullDescription) {
this.fullDescription = fullDescription;
}
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public ArrayList<ImageModel> getImageList() {
return imageList;
}
public void setImageList(ArrayList<ImageModel> imageList) {
this.imageList = imageList;
}
}
创建您的 ImageModel class
private class ImageModel {
@SerializedName("attachment")
private String attachment;
@SerializedName("position")
private int position;
public String getAttachment() {
return attachment;
}
public void setAttachment(String attachment) {
this.attachment = attachment;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
}
并通过您的 class 改造实施
@Headers("Content-Type: application/json")
@POST("api/ShopData/AddProduct")
Call<AddNewProductResponse> addNewProduct(@Header("Authorization") String authorization, @Body ModelClass jsonObject);
希望对您有所帮助。
当我通过邮递员发送请求作为行请求时(Json)我得到了成功响应,但是从我的 android 设备它没有成功。
邮递员请求
这是我的 Android 代码,
界面ApiService.Class
@Headers("Content-Type: application/json")
@POST("api/ShopData/AddProduct")
Call<AddNewProductResponse> addNewProduct(@Header("Authorization") String authorization, @Body JsonObject jsonObject);
Class ApiClient.Class
public static Call<AddNewProductResponse> addNewProduct(String token, JsonObject jsonObject) {
token = "Bearer " + token;
Call<AddNewProductResponse> call = apiService.addNewProduct(token, jsonObject);
return call;
}
API 通话
private void addNewProductToServer(JsonObject jsonObject) {
String token = AppSettings.getInstance(getActivity()).getStringValue(PrefKeys.token);
if (token != null && jsonObject != null) {
ApiClient.addNewProduct(token, jsonObject).enqueue(new Callback<AddNewProductResponse>() {
@Override
public void onResponse(Call<AddNewProductResponse> call, retrofit2.Response<AddNewProductResponse> response) {
if (response != null && response.body() != null) {
if (response.body().getMessage().getCode() == 1) {
showProductAddedSuccessDialog();
} else
Helper.showAlertDialogOK(getActivity(), Helper.getErrorMessages(response.body().getMessage().getCode()));
} else
Helper.showServerErrorDialog(getActivity());
}
@Override
public void onFailure(Call<AddNewProductResponse> call, Throwable t) {
Helper.showServerErrorDialog(getActivity());
}
});
}
}
创建请求Json对象,
JsonObject jsonObject = new JsonObject();
JsonArray images = new JsonArray();
try {
if (productImage != null) {
JsonObject imgObject = new JsonObject();
imgObject.addProperty("attachment", productImage.getAttachment());
imgObject.addProperty("position", 1);
images.add(imgObject);
}
jsonObject.addProperty("name", edtTxtProductName.getText().toString().trim());
jsonObject.addProperty("short_description", edtTxtShortDescription.getText().toString().trim());
jsonObject.addProperty("full_description", edtTxtLongDescription.getText().toString().trim());
jsonObject.addProperty("sku", edtTxtSku.getText().toString().trim());
jsonObject.addProperty("stock_quantity", Integer.parseInt(edtTxtStockQuantity.getText().toString().trim()));
jsonObject.addProperty("price", Float.parseFloat(edtTxtPrice.getText().toString().trim()));
jsonObject.addProperty("images", String.valueOf(images));
addNewProductToServer(jsonObject);
} catch (Exception e) {
e.printStackTrace();
}
您需要替换行
jsonObject.addProperty("images", String.valueOf(images));
来自
jsonObject.add("images", images);
创建您的模型类并设置您的值
public class ModelClass {
@SerializedName("name")
private String name = "";
@SerializedName("short_description")
private String shortDesc = "";
@SerializedName("full_description")
private String fullDescription = "";
@SerializedName("sku")
private String sku = "";
@SerializedName("stock_quantity")
private int qty = 0;
@SerializedName("price")
private double price = 0.0;
@SerializedName("images")
private ArrayList<ImageModel> imageList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getShortDesc() {
return shortDesc;
}
public void setShortDesc(String shortDesc) {
this.shortDesc = shortDesc;
}
public String getFullDescription() {
return fullDescription;
}
public void setFullDescription(String fullDescription) {
this.fullDescription = fullDescription;
}
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public ArrayList<ImageModel> getImageList() {
return imageList;
}
public void setImageList(ArrayList<ImageModel> imageList) {
this.imageList = imageList;
}
}
创建您的 ImageModel class
private class ImageModel {
@SerializedName("attachment")
private String attachment;
@SerializedName("position")
private int position;
public String getAttachment() {
return attachment;
}
public void setAttachment(String attachment) {
this.attachment = attachment;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
}
并通过您的 class 改造实施
@Headers("Content-Type: application/json")
@POST("api/ShopData/AddProduct")
Call<AddNewProductResponse> addNewProduct(@Header("Authorization") String authorization, @Body ModelClass jsonObject);
希望对您有所帮助。