对于一种类型

时间:2016-07-10 10:24:21

标签: java android gson

我收到的回复包含两种类型的对象:PaginationArtistSearch列表。我用于反序列化的模型是:

public class ArtistSearchResults {
List<ArtistSearch> artistSearchList;
Pagination pagination;

// getters and setters...

}

我在这里反序列化:

            Gson gson = new Gson();
            ArtistSearchResults results = gson.fromJson(response.toString(), ArtistSearchResults.class);
            List<ArtistSearch> artistSearchList = results.getArtistSearchList();
            Pagination pagination = results.getPagination();

            Log.i(TAG, "onSuccess Pagination size == " + pagination.getItems() );
            Log.i(TAG, "onSuccess RESULTS size == " + artistSearchList.get(0).getTitle() );

虽然我得到了Pagination对象的响应,但我总是为artistSearchList获取null。

我没有收到任何错误消息,所以我不确定错误在哪里。 我得到的结果看起来像这样:

07-10 06:11:06.726 6954-6954/jb.ti.discogsball I/SEARCHHANDLER: onSuccess Search-Artist response = {"pagination":{"per_page":5,"pages":3,"page":1,"urls":{"last":"https:\/\/api.discogs.com\/database\/search?q=savage+republic&per_page=5&type=artist&page=3","next":"https:\/\/api.discogs.com\/database\/search?q=savage+republic&per_page=5&type=artist&page=2"},"items":14},"results": [{"thumb":"https:\/\/api-img.discogs.com\/RAHE1vqHWb1xwPUv5y2Q1v5g4Yo=\/150x150\/smart\/filters:strip_icc():format(jpeg):mode_rgb():quality(40)\/discogs-images\/A-121133-1262786660.jpeg.jpg","title":"Savage Republic","uri":"\/artist\/121133-Savage-Republic","resource_url":"https:\/\/api.discogs.com\/artists\/121133","type":"artist","id":121133},{"thumb":"https:\/\/api-img.discogs.com\/GYkPyAYZEcFM0bzyjnBple7P3Yw=\/150x150\/smart\/filters:strip_icc():format(jpeg):mode_rgb():quality(40)\/discogs-images\/A-307086-1160815350.jpeg.jpg","title":"Bruce Licher","uri":"\/artist\/307086-Bruce-Licher","resource_url":"https:\/\/api.discogs.com\/artists\/307086","type":"artist","id":307086},{"thumb":"https:\/\/api-img.discogs.com\/XHvbh885CZ2uTcq7WT3ph0n0gMs=\/150x150\/smart\/filters:strip_icc():format(jpeg):mode_rgb():quality(40)\/discogs-images\/A-71224-1334785233.jpeg.jpg","title":"Medicine (2)","uri":"\/artist\/71224-Medicine-2","resource_url":"https:\/\/api.discogs.com\/artists\/71224","type":"artist","id":71224},{"thumb":"","title":"Val Haller","uri":"\/artist\/492425-Val-Haller","resource_url":"https:\/\/api.discogs.com\/artists\/492425","type":"artist","id":492425},{"thumb":"","title":"Ramona Clarke","uri":"\/artist\/1957827-Ramona-Clarke","resource_url":"https:\/\/api.discogs.com\/artists\/1957827","type":"artist","id":1957827}]}
07-10 06:11:06.732 6954-6954/jb.ti.discogsball I/SEARCHHANDLER: onSuccess Pagination size == 14

正如您所见,我得到了Pagination对象的值,但我得到了artistSearchList的NPE。

我用于ArtistSearch的模型是:

public class ArtistSearch {
int id;
String title;
String type;
String resource_url;
String uri;
String thumb;

// getters and setters ...

这是解析为null的响应中的文本:

  

&#34;结果&#34 ;: [{&#34;拇指&#34;:&#34; HTTPS://api-img.discogs.com/RAHE1vqHWb1xwPUv5y2Q1v5g4Yo=/150x150/smart/filters:strip_icc( ):格式(JPEG):mode_rgb():质量(40)/discogs-images/A-121133-1262786660.jpeg.jpg",&#34;标题&#34;:&#34;野蛮   共和国&#34;&#34; URI&#34;:&#34; /艺术家/ 121133-野蛮-共和国&#34;&#34; resource_url&#34;:&#34; HTTPS://api.discogs的.com /艺术家/ 121133&#34;&#34;类型&#34;:&#34;艺术家&#34;&#34; ID&#34;:121133},{&#34;拇指&#34 ;: &#34; HTTPS://api-img.discogs.com/GYkPyAYZEcFM0bzyjnBple7P3Yw=/150x150/smart/filters:strip_icc():格式(JPEG):mode_rgb():质量(40)/ discogs图像/ A-307086 -1160815350.jpeg.jpg&#34;&#34;标题&#34;:&#34;布鲁斯   Licher&#34;&#34; URI&#34;:&#34; /艺术家/ 307086-布鲁斯Licher&#34;&#34; resource_url&#34;:&#34; HTTPS://api.discogs的.com /艺术家/ 307086&#34;&#34;类型&#34;:&#34;艺术家&#34;&#34; ID&#34;:307086},{&#34;拇指&#34 ;: &#34; HTTPS://api-img.discogs.com/XHvbh885CZ2uTcq7WT3ph0n0gMs=/150x150/smart/filters:strip_icc():格式(JPEG):mode_rgb():质量(40)/ discogs图像/ A-71224 -1334785233.jpeg.jpg&#34;&#34;标题&#34;:&#34;医药   (2)&#34;&#34; URI&#34;:&#34; /艺术家/ 71224-医药-2&#34;&#34; resource_url&#34;:&#34; https://开头api.discogs.com/artists/71224","type":"artist","id":71224},{"thumb&# 34;:&#34;&#34;&#34;标题&#34;:&#34;缬氨酸   哈勒&#34;&#34; URI&#34;:&#34; /艺术家/ 492425-VAL-哈勒&#34;&#34; resource_url&#34;:&#34; HTTPS://api.discogs的.com /艺术家/ 492425&#34;&#34;类型&#34;:&#34;艺术家&#34;&#34; ID&#34;:492425},{&#34;拇指&#34 ;: &#34;&#34;&#34;标题&#34;:&#34;拉马纳   克拉克&#34;&#34; URI&#34;:&#34; /艺术家/ 1957827-拉马纳 - 克拉克&#34;&#34; resource_url&#34;:&#34; HTTPS://api.discogs的.com /艺术家/ 1957827&#34;&#34;类型&#34;:&#34;艺术家&#34;&#34; ID&#34;:1957827}]

1 个答案:

答案 0 :(得分:2)

请您在ArtistSearchResults.java中替换以下变量声明:

private List<ArtistSearch> artistSearchList;

由此:

@SerializedName("results")
private List<ArtistSearch> artistSearchList;

并查看结果?