我收到一个包含两种类型的对象的响应: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的文本:


  “结果”:[{“拇指”:“ 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“:”野蛮人
  Republic”,“ uri”:“ / artist / 121133-Savage-Republic”,“ resource_url”:“ https://api.discogs.com/artists/121133”,“ type”:“ artist”,“ id”: 121133},{“缩略图”:“ 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},{“拇指”:“ 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“:”医学
  (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}]

最佳答案

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

private List<ArtistSearch> artistSearchList;

这样:

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


看看结果吗?

08-18 20:08