使用Retrofit和Simple XML Converter获取XML数据

时间:2017-02-28 20:53:48

标签: java android xml retrofit2

我需要从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 &amp; 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 &amp; 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 &amp; 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;
}

这是Retrofit界面

 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。

以下是我打电话的方式

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

1 个答案:

答案 0 :(得分:1)

我认为您应该更改this.bind.addToCart(this)中返回的ProductDto类型 通过与您的xml匹配的类型,等待作为根元素的封闭类型包含多个产品而不仅仅是产品 您可以介绍getProduct(),这是一个包含ArrayOfProduct List的包装类。

你的方法可能如此:

ProductDto