Android 如何解决org.simpleframework.xml.core.ValueRequiredException:无法满足@org.simpleframework.xml.ElementList()的要求

Android 如何解决org.simpleframework.xml.core.ValueRequiredException:无法满足@org.simpleframework.xml.ElementList()的要求,android,xml,xml-serialization,retrofit2,simple-framework,Android,Xml,Xml Serialization,Retrofit2,Simple Framework,我正试图通过改型从URL获取图书xml数据。但当应用程序运行时,它会显示错误。我是新来的,所以请帮助我。以下是错误消息: 02-20 23:06:37.943 23835-23835/com.santossingh.reader E/error: org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, em

我正试图通过改型从URL获取图书xml数据。但当应用程序运行时,它会显示错误。我是新来的,所以请帮助我。以下是错误消息:

    02-20 23:06:37.943 23835-23835/com.santossingh.reader E/error:
org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=true, name=work, required=true, type=void) on field 'works' public java.util.List com.santossingh.reader.AWS.GoodReadsModels.Results.works for class com.santossingh.reader.AWS.GoodReadsModels.Results at line 2
以下是XML文件结构以及我的模型和类:

1-URL XML文件的结构:

<?xml version="1.0" encoding="UTF-8"?>
        <GoodreadsResponse>
          <Request>
            <authentication>true</authentication>
              <key><![CDATA[xxxxx]]></key>
            <method><![CDATA[search_index]]></method>
          </Request>
          <search>
          <query><![CDATA[business]]></query>
            <results-start>1</results-start>
            <results-end>20</results-end>
            <total-results>109755</total-results>
            <source>Goodreads</source>
            <query-time-seconds>0.22</query-time-seconds>
            <results>
                <work>
          <id type="integer">17624817</id>
          <books_count type="integer">85</books_count>
          <ratings_count type="integer">156992</ratings_count>
          <text_reviews_count type="integer">8489</text_reviews_count>
          <original_publication_year type="integer">2011</original_publication_year>
          <original_publication_month type="integer" nil="true"/>
          <original_publication_day type="integer" nil="true"/>
          <average_rating>4.01</average_rating>
          <best_book type="Book">
            <id type="integer">12609433</id>
            <title>The Power of Habit: Why We Do What We Do in Life and Business</title>
            <author>
              <id type="integer">5201530</id>
              <name>Charles Duhigg</name>
            </author>
            <image_url>https://images.gr-assets.com/books/1366758683m/12609433.jpg</image_url>
            <small_image_url>https://images.gr-assets.com/books/1366758683s/12609433.jpg</small_image_url>
          </best_book>
        </work>
      <results>

我找到了答案。在Result.class中,只需更改@ElementList()参数。以下是解决方案:

错误-

    @ElementList(name = "work", inline = true)
    public List<Work> works;

我也面临过这个问题。查看文档,它说的如下

The ValueRequiredException is thrown when an attribute or element is missing from the XML document. This is thrown only if the attribute or element is required according to the annotation for that field within the XML schema class.
基本上,当形成的请求与XML请求模式不匹配时,就会发生这种情况

因此,请使用注释

@ElementList(name="XMLNode", required = false)
public <<DataType>> XMLNode;
@ElementList(name=“XMLNode”,required=false)
公共XMLNode;
required=false
时,构建器将忽略标记并构建请求的其余节点

附言:
这在使用同一类来形成XML请求以及解析响应时非常有用。

您知道指定
inline=true
的含义吗?inline=true您指定要解析的元素位于目标元素的同一父树中-它们不是嵌套子元素。您可以查看文档中的示例。我得到这个org.simpleframework.xml.core.PersistenceException:Element“item”已用于@org.simpleframework.xml.ElementList错误。我在通道标记内嵌套了项。请注意,如果您有任何帮助,请在此处进行调整-有时XML是“格式良好的”,但在某些元素上缺少属性或值,但并非所有元素都缺少属性或值。required=false标志适用于这些情况。这个答案给出了设置required=false的原因,这是解决问题的正确方法
    @ElementList(inline = true, required = false)
    public List<Work> works;
1- inline=true
2- required=false
example : @ElementList(inline = true, required = false)
The ValueRequiredException is thrown when an attribute or element is missing from the XML document. This is thrown only if the attribute or element is required according to the annotation for that field within the XML schema class.
@ElementList(name="XMLNode", required = false)
public <<DataType>> XMLNode;