在Android中为Goodreads API响应创建POJO类

时间:2016-12-08 21:00:56

标签: java android xml pojo

我想在Android中使用simplexml和Retrofit 2创建POJO类,但我一直坚持为Search API创建Model类。

以下是xml中的响应示例,我想映射到POJO类。

<GoodreadsResponse>
   <Request>
      <authentication>true</authentication>
      <key><![CDATA[]]></key>
      <method><![CDATA[search_search]]></method>
   </Request>
   <search>
      <query>...</query>
      <results-start>1</results-start>
      <results-end>20</results-end>
      <total-results>491</total-results>
      <source>Goodreads</source>
      <query-time-seconds>0.14</query-time-seconds>
      <results>
         <work>
            <id type="integer">2422333</id>
            <books_count type="integer">214</books_count>
            <ratings_count type="integer">810505</ratings_count>
            <text_reviews_count type="integer">36595</text_reviews_count>
            <original_publication_year type="integer">1985</original_publication_year>
            <original_publication_month type="integer" nil="true" />
            <original_publication_day type="integer" nil="true" />
            <average_rating>4.29</average_rating>
            <best_book type="Book">
               <id type="integer">375802</id>
               <title>Ender's Game (Ender's Saga, #1)</title>
               <author>
                  <id type="integer">589</id>
                  <name>Orson Scott Card</name>
               </author>
               <image_url>https://images.gr-assets.com/books/1408303130m/375802.jpg</image_url>
               <small_image_url>https://images.gr-assets.com/books/1408303130s/375802.jpg</small_image_url>
            </best_book>
         </work>
         <work>
            <id type="integer">938064</id>
            <books_count type="integer">54</books_count>
            <ratings_count type="integer">69233</ratings_count>
            <text_reviews_count type="integer">586</text_reviews_count>
            <original_publication_year type="integer">1984</original_publication_year>
            <original_publication_month type="integer">12</original_publication_month>
            <original_publication_day type="integer" nil="true" />
            <average_rating>4.16</average_rating>
            <best_book type="Book">
               <id type="integer">44687</id>
               <title>Enchanters' End Game (The Belgariad, #5)</title>
               <author>
                  <id type="integer">8732</id>
                  <name>David Eddings</name>
               </author>
               <image_url>https://images.gr-assets.com/books/1217735909m/44687.jpg</image_url>
               <small_image_url>https://images.gr-assets.com/books/1217735909s/44687.jpg</small_image_url>
            </best_book>
         </work>
      </results>
   </search>
</GoodreadsResponse>

到目前为止,我的模型课程并不起作用。在改造中我得到了一个响应,但它没有映射到创建的模型:

@Root(strict=false)
public class GoodreadsResponse {

    @Element(name = "work")
    public static class Work {
        @Element
        int id;
    }


    @Element(name = "search")
    public static class Search {
        @Element
        Results results;
    }
    @Element(name = "results")
    public static class Results {
        @ElementList(name = "results", entry = "work")
        ArrayList<Work> results;
    }

}

我有兴趣只为每本书获取元素工作和image_url中的书籍列表。

我非常感谢你的帮助。

0 个答案:

没有答案