我需要从SupermarketApi获取数据。这是使用Postman原始数据的样子。
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfProduct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.SupermarketAPI.com">
<Product>
<Itemname>Flake Parsley Seasoning - 0.375 Oz. Plastic Peg Bag</Itemname>
<ItemDescription>Flake Parsley Seasoning - 0.375 Oz. Plastic Peg Bag</ItemDescription>
<ItemCategory>Condiments/Spices & Bake</ItemCategory>
<ItemID>84309</ItemID>
<ItemImage>http://smapistorage.blob.core.windows.net/thumbimages/2/3C07125.jpg</ItemImage>
<AisleNumber>Aisle:N/A</AisleNumber>
</Product>
<Product>
<Itemname>El Guapo Flake Parsley Spice - 0.25 Oz. Plastic Bag</Itemname>
<ItemDescription>El Guapo Flake Parsley Spice - 0.25 Oz. Plastic Bag</ItemDescription>
<ItemCategory>Condiments/Spices & Bake</ItemCategory>
<ItemID>89721</ItemID>
<ItemImage>http://smapistorage.blob.core.windows.net/thumbimages/2/no_image_sm.jpg</ItemImage>
<AisleNumber>Aisle:N/A</AisleNumber>
</Product>
<Product>
<Itemname>Superline Deal Flake Parsley Seasoning - 1.2 Oz Shaker</Itemname>
<ItemDescription>Superline Deal Flake Parsley Seasoning - 1.2 Oz Shaker</ItemDescription>
<ItemCategory>Condiments/Spices & Bake</ItemCategory>
<ItemID>85817</ItemID>
<ItemImage>http://smapistorage.blob.core.windows.net/thumbimages/2/5E59BCF.jpg</ItemImage>
<AisleNumber>Aisle:N/A</AisleNumber>
</Product>
</ArrayOfProduct>
这是我对这些数据的POJO表示。
public class ProductDto {
@Element(name = "itemID")
private String itemID;
@Element(name = "Itemname")
private String Itemname;
@Element(name = "AisleNumber")
private String AisleNumber;
@Element(name = "ItemCategory")
private String ItemCategory;
@Element(name = "ItemImage")
private String ItemImage;
@Element(name = "ItemDescription")
private String ItemDescription;
}
这是翻新界面
public interface SuperMarketApiService {
public static final String BASE_URL = "http://www.SupermarketAPI.com/api.asmx/";
@GET("SearchByProductName")
Call<ProductDto> getProduct(
@Query("APIKEY") String key,
@Query("ItemName") String itemName
);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();
}
问题是当我打电话时,我收到错误消息
org.simpleframework.xml.core.ElementException:元素“ Product”在第3行的Data.ProductDto类中没有匹配项
如何更新POJO以将XML ArrayOfProducts正确映射到Java ArrayList of Products。
这是我拨打电话的方式
private void getProducts() {
SuperMarketApiService apiService = SuperMarketApiService.retrofit.create(SuperMarketApiService.class);
Call<ArrayOfProduct> call = apiService.getProduct("jdmdldfdd", "Parsley");
call.enqueue(new Callback<ArrayOfProduct>() {
@Override
public void onResponse(Call<ArrayOfProduct> call, Response<ArrayOfProduct> response) {
ArrayOfProduct products = response.body();
Log.d(LOG_TAG, "Product Count: " + products.getProductDtos().size());
}
@Override
public void onFailure(Call<ArrayOfProduct> call, Throwable t) {
Log.d(LOG_TAG, t.getLocalizedMessage());
}
});
}
最佳答案
我认为您应该在ProductDto
中更改返回的getProduct()
类型
通过与您的xml匹配的类型,该xml将包含多个产品而不只是一个产品的封闭类型作为根元素。
您可以引入ArrayOfProduct
,这是一个包装类,其中包含List
的ProductDto
。
您的方法可能是这样的:
@GET("SearchByProductName")
Call<ArrayOfProduct> getProducts(
@Query("APIKEY") String key,
@Query("ItemName") String itemName
);