如何在retrofit2和SimpleXmlConverterFactory中解析xml数据?

时间:2017-09-19 07:03:46

标签: android xml retrofit retrofit2 simple-framework

我想知道如何解决class =" array"在XML中。请帮助我!!

<?xml version="1.0" encoding="UTF-8"?>
<RFC30>
    <code type="string">000</code>
    <msg type="string">OK</msg>
    <routeList class="array">
        <list class="object">
            <reMark type="string"/>
            <searchFld type="string"/>
            <searchNm type="string"/>
            <stopId type="string">30693</stopId>
            <stopKname type="string">BYC</stopKname>
            <stopStandardid type="string">312100693</stopStandardid>
            <stopX type="string">127.00796999</stopX>
            <stopY type="string">35.82071324</stopY>
        </list>
        <list class="object">
            <reMark type="string"/>
            <searchFld type="string"/>
            <searchNm type="string"/>
            <stopId type="string">30690</stopId>
            <stopKname type="string">BYC</stopKname>
            <stopStandardid type="string">312100690</stopStandardid>
            <stopX type="string">127.00778106</stopX>
            <stopY type="string">35.82043092</stopY>
        </list>
        <list class="object">
            <reMark type="string"/>
            <searchFld type="string"/>
            <searchNm type="string"/>
            <stopId type="string">30683</stopId>
            <stopKname type="string">CTS전북방송</stopKname>
            <stopStandardid type="string">305100683</stopStandardid>
            <stopX type="string">127.14640711</stopX>
            <stopY type="string">35.8199967</stopY>
        </list>
    </routeList>
</RFC30>

这是XML数据。

BusStopListInfo.class

Root(name = "RFC30")
public class BusStopListInfo {
    @Element(name = "code")
    private String code;
    @Element(name = "msg")
    private String msg;
    @ElementList(inline = true)
    private ArrayList<BusStopList> routeList;
}

BusStopList.class

@Root(name = "routeList")
public class BusStopList {
    @Element(name = "list")
    private BusStopData list;
}

BusStopData.class

@Root(name = "list")
public class BusStopData {

    @Element(name = "reMark")
    private String reMark;
    @Element(name = "searchFld")
    private String searchFld;
    @Element(name = "searchNm")
    private String searchNm;
    @Element(name = "stopId")
    private String stopId;
    @Element(name = "stopKname")
    private String stopKname;
    @Element(name = "stopStandardId")
    private String stopStandardid;
    @Element(name = "stopX")
    private String stopX;
    @Element(name = "stopY")
    private String stopY;
}

但是,错误是......

  

致命的例外:主要                                                                               处理:com.example.mkr.jeonju_bus,PID:29458                                                                               java.lang.RuntimeException:java.lang.ClassNotFoundException:Didn&t; t   找到课程&#34;数组&#34;在路径上:DexPathList [[zip文件   &#34; /data/app/com.example.mkr.jeonju_bus-2/base.apk"],nativeLibraryDirectories = [/数据/应用/ com.example.mkr.jeonju_bus-2 / LIB /臂,   / vendor / lib,/ system / lib]]                                                                                   在   retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:44)                                                                                   在   retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:23)                                                                                   at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:119)                                                                                   at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:218)                                                                                   at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)                                                                                   在   retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41)                                                                                   at io.reactivex.Observable.subscribe(Observable.java:10179)                                                                                   在   retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)                                                                                   at io.reactivex.Observable.subscribe(Observable.java:10179)                                                                                   在   io.reactivex.internal.operators.observable.ObservableSingleMaybe.subscribeActual(ObservableSingleMaybe.java:30)                                                                                   在io.reactivex.Maybe.subscribe(Maybe.java:3540)                                                                                   在   io.reactivex.internal.operators.maybe.MaybeSubscribeOn $ SubscribeTask.run(MaybeSubscribeOn.java:54)                                                                                   at io.reactivex.Scheduler $ 1.run(Scheduler.java:134)                                                                                   在   io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:59)                                                                                   在   io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:51)                                                                                   在java.util.concurrent.FutureTask.run(FutureTask.java:237)                                                                                   在   java.util.concurrent.ScheduledThreadPoolExecutor中的$ ScheduledFutureTask.access $ 201(ScheduledThreadPoolExecutor.java:154)                                                                                   在   java.util.concurrent.ScheduledThreadPoolExecutor中的$ ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)                                                                                   在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)                                                                                   在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:588)                                                                                   在java.lang.Thread.run(Thread.java:818)                                                                                引起:java.lang.ClassNotFoundException:没找到类&#34;数组&#34;   在路径上:DexPathList [[zip文件   &#34; /data/app/com.example.mkr.jeonju_bus-2/base.apk"],nativeLibraryDirectories = [/数据/应用/ com.example.mkr.jeonju_bus-2 / LIB /臂,   / vendor / lib,/ system / lib]]                                                                                   在   dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)                                                                                   at java.lang.ClassLoader.loadClass(ClassLoader.java:511)                                                                                   at java.lang.ClassLoader.loadClass(ClassLoader.java:469)                                                                                   at org.simpleframework.xml.strategy.Loader.load(Loader.java:50)                                                                                   在   org.simpleframework.xml.strategy.TreeStrategy.readValue(TreeStrategy.java:163)                                                                                   在   org.simpleframework.xml.strategy.TreeStrategy.read(TreeStrategy.java:102)                                                                                   at org.simpleframework.xml.core.Source.getOverride(Source.java:360)                                                                                   在   org.simpleframework.xml.core.Factory.getConversion(Factory.java:207)                                                                                   at org.simpleframework.xml.core.Factory.getOverride(Factory.java:139)                                                                                   在   org.simpleframework.xml.core.ObjectFactory.getInstance(ObjectFactory.java:63)                                                                                   at org.simpleframework.xml.core.Composite.read(Composite.java:139)                                                                                   at org.simpleframework.xml.core.Traverser.read(Traverser.java:92)                                                                                   在   org.simpleframework.xml.core.CompositeInlineList.read(CompositeInlineList.java:190)                                                                                   在   org.simpleframework.xml.core.CompositeInlineList.read(CompositeInlineList.java:167)                                                                                   在   org.simpleframework.xml.core.CompositeInlineList.read(CompositeInlineList.java:124)                                                                                   在   org.simpleframework.xml.core.Composite.readVariable(Composite.java:623)                                                                                   在   org.simpleframework.xml.core.Composite.readInstance(Composite.java:573)                                                                                   在   org.simpleframework.xml.core.Composite.readUnion(Composite.java:549)                                                                                   在   org.simpleframework.xml.core.Composite.readElement(Composite.java:532)                                                                                   在   org.simpleframework.xml.core.Composite.readElements(Composite.java:445)                                                                                   在   org.simpleframework.xml.core.Composite.access $ 400(Composite.java:59)                                                                                   在   org.simpleframework.xml.core.Composite $ Builder.read(Composite.java:1383)                                                                                   at org.simpleframework.xml.core.Composite.read(Composite.java:201)                                                                                 在org.simpleframework.xml.core.Composite.read

1 个答案:

答案 0 :(得分:1)

问题在于您的XML定义了一个Permission.objects.get(codename='add_user', content_type__app_label='users', content_type__model='user') 属性,SimpleXML将使用该属性来确定正在读取的内容的确切类类型。显然没有名为class的类存在,所以它失败并出现异常。

但是,array类可以在构造函数中使用Persister进行初始化,例如,您可以使用Strategy来忽略AnnotationStrategy属性,并检查{ {1}}注释。

请参阅the CAKE documentation@ConvertAnnotationStrategyStrategy

在您的情况下,您很可能需要编写自己的自定义策略,请参阅默认的Persister作为示例。