使用两个查询使用RetroFit加载JSON数据时出现NullPointerException

时间:2018-04-16 14:51:23

标签: java android json retrofit2

我正在开发一个应用程序,当用户搜索名称时,它会从API中提取JSON格式的数据,并且可以在搜索艺术家或专辑之间进行选择。我正在使用RetroFit来调用JSON数据,并希望在我的界面中使用两个查询。

调用JSON数据的代码:

public void ArtistSearch() {
    String searchTerm = searchTxt.getText().toString().trim();
    String searchCat = searchPick.getSelectedItem().toString().trim();
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    Retrofit.Builder builder = new Retrofit.Builder().baseUrl(api_url).addConverterFactory
            (GsonConverterFactory.create());
    final Retrofit retrofit = builder.client(httpClient.build()).build();
    ArtistClient client = retrofit.create(ArtistClient.class);
    Call<JSONResponse> call = client.getArtistList(searchTerm, searchCat);
    call.enqueue(new Callback<JSONResponse>() {
        @Override
        public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
            List<ArtistList> artists = response.body().getArtists();
            Log.d(TAG, "Artists found");
            Log.d(TAG, "Number of artists:" + artists.size());
            artistAdapter = new ArtistAdapter(DiscogSearchPage.this, artists);
            discogList.setAdapter(artistAdapter);
        }

        @Override
        public void onFailure(Call<JSONResponse> call, Throwable t) {
            Log.e(TAG, "Fail: " + t.getMessage());
        }
    });
}

接口:

public interface ArtistClient {

    @GET("/database/search")
    Call<JSONResponse> getArtistList (@Query("q") String searchTerm, @Query("type") String searchCategory);
}

目前,当我尝试运行此操作时,我收到错误

  

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.util.List ... Responses.JSONResponse.getArtists()'。

但是,如果我在界面中注释掉第二个查询并从代码中删除searchCat,则数据加载正常。我不知道发生了什么。

感谢任何帮助。

编辑:对于那些要求模型类和JSON响应的人。

JSON响应类:

public class JSONResponse {

    @SerializedName("results")
    @Expose
    private List<ArtistList> artists = null;
    // Getters and setters

ArtistList类:

public class ArtistList {

    @SerializedName("thumb")
    @Expose
    private String thumb;
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("uri")
    @Expose
    private String uri;
    @SerializedName("cover_image")
    @Expose
    private String coverImage;
    @SerializedName("resource_url")
    @Expose
    private String resourceUrl;
    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("id")
    @Expose
    private int id;
    // Getters and setters

JSON响应,搜索“Drake”并输入“artist”:

{
  "pagination": {
    "per_page": 50,
    "items": 22893,
    "page": 1,
    "urls": {
      "last": "https://api.discogs.com/database/search?%3Ftype=artist&q=drake&secret=AEmHdfwlGwPqUYQpTBVrarEtzjKsykih&key=wMWTrOWaTkfHDUQVXFSG&per_page=50&page=458",
      "next": "https://api.discogs.com/database/search?%3Ftype=artist&q=drake&secret=AEmHdfwlGwPqUYQpTBVrarEtzjKsykih&key=wMWTrOWaTkfHDUQVXFSG&per_page=50&page=2"
    },
    "pages": 458
  },
  "results": [
    {
      "thumb": "https://img.discogs.com/Voe5_n4NEBvrIW2AczQyGb389WM=/150x150/smart/filters:strip_icc():format(jpeg):mode_rgb():quality(40)/discogs-images/A-151199-1520497976-7068.jpeg.jpg",
      "title": "Drake",
      "uri": "/artist/151199-Drake",
      "cover_image": "https://img.discogs.com/nFMZ1bVcXA3bLHxR_0LVSrRB7iM=/456x615/smart/filters:strip_icc():format(jpeg):mode_rgb():quality(90)/discogs-images/A-151199-1520497976-7068.jpeg.jpg",
      "resource_url": "https://api.discogs.com/artists/151199",
      "type": "artist",
      "id": 151199
    },
    // results objects continue

0 个答案:

没有答案