如何为lastfm相册构建pojo类.search json响应解析

How to build an pojo class for a lastfm album.search json response parse

本文关键字:search json 响应 pojo 构建 lastfm      更新时间:2023-09-26

我这里有Json

http://ws.audioscrobbler.com/2.0/?method=album.search&专辑=+精华+的+查理+帕克&limit=1&api_key=26980b71e5c813da2c7e0156afdddd4f&format=json

我制作了这个pojo类来解析它

import java.util.List;
public class LastFmAlbumInfo {
    public Results results;
    public class Results {
        QueryInfo queryInfo;
        String totalResults;
        String startIndex;
        String itemsPerPage;
        AlbumMatches albumMatches;
        Attribute attribute;
        public class QueryInfo {
            String text;
            String role;
            String searchTerms;
            String startPage;
        }
        public class AlbumMatches {
            List<Album> albums;
            public class Album {
                String name;
                String artist;
                String url;
                List<Image> images;
                String streamable;
                String mbid;
                public class Image {
                    String imageUrl;
                    String imageSizeDescription;
                }
            }
        }
        public class Attribute {
            String _for;
        }
    }
}

当我引用一个子属性时,例如

response.results.totalResults

我收到这个错误

java.lang.NullPointerException:尝试从空对象引用上的字段"java.util.List com.example.michael.musicplayer5.LastFmAlbumInfo$Results$AlbumMatches.albums"读取

我不确定我的pojo课出了什么问题。

我正在使用Volley进行GET请求

    String requestUrl = getUrl(albumObject.albumTitle);
    getRequestQueue();
    GsonRequest<LastFmAlbumInfo> myReq = new GsonRequest<>(
            Request.Method.GET,
            requestUrl,
            LastFmAlbumInfo.class,
            null,
            createMyReqSuccessListener(requestUrl, albumObject),
            createMyReqErrorListener(requestUrl));
    mRequestQueue.add(myReq);

我搞定了!

import com.google.gson.annotations.SerializedName;
import java.util.List;
public class LastFmAlbumInfo {
    @SerializedName("results")
    public Results results;
    public class Results {
        //@SerializedName(“opensearch:Query”)
        //QueryInfo opensearch;
        @SerializedName("opensearch:totalResults")
        String totalResults;
        @SerializedName("opensearch:startIndex")
        String startIndex;
        @SerializedName("opensearch:itemsPerPage")
        String itemsPerPage;
        @SerializedName("albummatches")
        AlbumMatches albumMatches;
        @SerializedName("@attr")
        Attribute attribute;
        public class QueryInfo {
            @SerializedName("#text")
            String text;
            @SerializedName("role")
            String role;
            @SerializedName("searchTerms")
            String searchTerms;
            @SerializedName("startPage")
            String startPage;
        }
        public class AlbumMatches {
            @SerializedName("album")
            List<Album> albums;
            public class Album {
                @SerializedName("name")
                String name;
                @SerializedName("artist")
                String artist;
                @SerializedName("url")
                String url;
                @SerializedName("image")
                List<Image> images;
                @SerializedName("streamable")
                String streamable;
                @SerializedName("mbid")
                String mbid;
                public class Image {
                    @SerializedName("#text")
                    String imageUrl;
                    @SerializedName("size")
                    String imageSizeDescription;
                }
            }
        }
        public class Attribute {
            @SerializedName("for")
            String _for;
        }
    }