【问题标题】:Retrofit cannot convert to xml to java.How to convert xml to java using retrofit2Retrofit 无法将 xml 转换为 java。如何使用 retrofit2 将 xml 转换为 java
【发布时间】:2016-04-18 05:25:08
【问题描述】:

您好我正在尝试使用改造来编写一个简单的休息客户端,我正在尝试调用返回 xml 响应的 REST API GET,但是改造无法解析 xml。

改造代码

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API_URL)
                .addConverterFactory(SimpleXmlConverterFactory.create())
                .build();
        // Create an instance of our OfferClient API interface.
        OfferClient offerClient = retrofit.create(OfferClient.class);

        // Create a call instance for looking up offers.
        String offerID="20000798";
        Call<Offer> call = offerClient.offers(offerID);
        Offer offer = call.execute().body();
        System.out.println("Offer Response..."+ offer.getDescription());

线程“主”java.lang.RuntimeException 中的异常: org.simpleframework.xml.core.ElementException:元素'featureSetID' 在课堂上没有比赛 com.intuit.schema.platform.webs.catalog.internal.offers.v3.Offer$Product 在第 1 行 在 retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:44) 在

无法解组的响应。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<offer xmlns="http://schema.intuit.com/platform/webs/catalog/internal/offers/v3">
    <offerID>20000798</offerID>
    <name>QBOA + QBO Plus + QBOP Enhanced CA CAD for Accountants</name>
    <description>QBOA + QBO Plus + QBOP Enhanced CA CAD for Accountants</description>
    <effectiveStartDate>2015-12-13T00:00:00-0800</effectiveStartDate>
    <currency>CAD</currency>
    <region>CA</region>
    <country>CA</country>
    <offerType>Free</offerType>
    <offerLevel>BASE</offerLevel>
    <customerSegment>ACCOUNTANT</customerSegment>
    <extendedCustomerSegment>REGULAR</extendedCustomerSegment>
    <salesChannel>DIRECT</salesChannel>
    <isListPriceOffer>true</isListPriceOffer>
    <product>
        <productID>22000220</productID>
        <packageId>21001070</packageId>
        <code>QBOA</code>
        <name>QuickBooks Online Accountant</name>
        <description>QuickBooks Online Accountant</description>
        <grantOfferingType>Intuit.smallbusiness.qba.web</grantOfferingType>
        <billingServiceType>/service/intuit/qboa</billingServiceType>
        <featureSet>
            <featureSetID>25001595</featureSetID>
            <code>QBOA_STANDARD</code>
            <name>QBO Accountant Standard</name>
            <status>Active</status>
            <description>QuickBooks Online Accountant Standard</description>
            <feature>
                <code>STANDARD</code>
                <name>Standard</name>
                <type>Feature Set</type>
            </feature>
        </featureSet>
        <charge>
            <chargeID>23001499</chargeID>
            <name>QuickBooks Online Accountant Standard</name>
            <description>QuickBooks Online Accountant Standard CA CAD Monthly Free</description>
            <type>Recurring</type>
        </charge>
    </product>
    <product>
        <productID>22000000</productID>
        <packageId>21001071</packageId>
        <code>QBO</code>
        <name>QuickBooks Online</name>
        <description>QuickBooks Online</description>
        <grantOfferingType>Intuit.sbe.salsa.default</grantOfferingType>
        <billingServiceType>/service/intuit/qbo</billingServiceType>
        <featureSet>
            <featureSetID>25000000</featureSetID>
            <code>QBO_PLUS</code>
            <name>QBO_PLUS</name>
            <status>Active</status>
            <description>QuickBooks Online Plus</description>
            <feature>
                <code>PLUS</code>
                <name>Plus</name>
                <type>Feature Set</type>
            </feature>
        </featureSet>
        <charge>
            <chargeID>23001500</chargeID>
            <name>QuickBooks Online Plus</name>
            <description>QuickBooks Online Plus Accountant CA CAD Monthly Free</description>
            <type>Recurring</type>
        </charge>
    </product>
    <product>
        <productID>22000154</productID>
        <dependentOnProductId>22000000</dependentOnProductId>
        <dependentOnProductCode>QBO</dependentOnProductCode>
        <packageId>21001323</packageId>
        <code>QBOP</code>
        <name>QuickBooks Online Payroll</name>
        <description>QuickBooks Online Payroll</description>
        <grantOfferingType>Intuit.ems.iop</grantOfferingType>
        <billingServiceType>/service/intuit/qbo/qbop</billingServiceType>
        <featureSet>
            <featureSetID>25000537</featureSetID>
            <code>ENHANCED</code>
            <name>ENHANCED</name>
            <status>Active</status>
            <description>ENHANCED</description>
            <feature>
                <code>ENHANCED</code>
                <name>Enhanced</name>
                <type>Feature Set</type>
            </feature>
        </featureSet>
        <charge>
            <chargeID>23001868</chargeID>
            <name>QuickBooks Online Payroll Enhanced</name>
            <description>QuickBooks Online Payroll Enhanced CA CAD Monthly Free</description>
            <type>Recurring</type>
        </charge>
        <charge>
            <chargeID>23001964</chargeID>
            <name>QuickBooks Online Payroll Enhanced Usage</name>
            <description>QuickBooks Online Payroll Enhanced Accountant CA CAD Usage Free</description>
            <type>Usage</type>
        </charge>
    </product>
    <transition>
        <transitionType>Resubscribe</transitionType>
        <fromOfferID>20000798</fromOfferID>
        <toOfferID>20000798</toOfferID>
    </transition>
</offer>

【问题讨论】:

    标签: retrofit2


    【解决方案1】:

    在改造构建器中将 addConverterFactory(SimpleXmlConverterFactory.create()) 替换为 addConverterFactory(SimpleXmlConverterFactory.createNonStrict())。这将使解析器能够容忍您的 POJO 中没有出现的 xml 标记

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-04
    • 2018-07-08
    • 1970-01-01
    • 2015-04-11
    • 2010-10-27
    • 2014-01-05
    • 2016-07-17
    • 1970-01-01
    相关资源
    最近更新 更多