如何为lastfm album.search json响应解析构建一个pojo类

时间:2015-12-22 22:30:10

标签: javascript android

我在这里有Json

http://ws.audioscrobbler.com/2.0/?method=album.search&album=the+essence+of+charlie+parker&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'on null object reference

我不确定我的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);

1 个答案:

答案 0 :(得分:0)

我明白了!

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;
        }
    }