首页 > 解决方案 > Retrofit 2 请求返回空对象引用

问题描述

安卓新手来了!我正在尝试读取这样的 JSON:

{  
"channel0":{  
  "song":"Crush",
  "artist":"Jennifer Paige",
  "duration":"180",
  "playedat":"1545065265",
  "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/8eecc92227fcbb09b43472f000df74e1.png"
},
"channel1":{  
  "song":"Reasons Why",
  "artist":"Brand New Immortals",
  "duration":"180",
  "playedat":"1545065371",
  "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/c059afda95dd35354af26cf72e5deab4.png"
},
"channel2":{  
  "song":"Dance Me To The End Of Love",
  "artist":"Leonard Cohen",
  "duration":"300",
  "playedat":"1545065181",
  "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/a368617dc7dc4716a9badb523ff6e7d4.png"
},
"channel3":{  
  "song":"4 Minutes",
  "artist":"Madonna",
  "duration":"180",
  "playedat":"1545065300",
  "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/1dcefc6496be4155a00f919dcbb54f77.png"
},
"channel4":{  
  "song":"Mothers, Sisters, Daughters",
  "artist":"Voxtrot",
  "duration":"180",
  "playedat":"1545065257",
  "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/5861b97231bd4ffe9218dfcacc27d68a.png"
}
}

来自使用 Retrofit 2 的 URL。我在 MainActivity 中使用了这个结构:

retrofit2.Call<List<Channel>> call = restInterface.getChannels();

call.enqueue(new Callback<List<Channel>>() {
    @Override
    public void onResponse(retrofit2.Call<List<Channel>> call, Response<List<Channel>> response) {
        List<Channel> channelList= new ArrayList<>();
        channelList=response.body();
        for (int i=0;i<4;i++){
            System.out.println(""+channelList.get(i).getArtist()+"\n");
        }
    }

当我运行我的应用程序时,我收到了这个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.korhan.frontend, PID: 30563
    java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
        at com.example.korhan.frontend.MainActivity$1.onResponse(MainActivity.java:44)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

顺便说一句,我只为 JSON 中的所有这些 Channel 使用了一个 Channel 类,但是当我尝试使用 POJO 创建器时,它为 JSON 类中的每个 Channel 创建了 4 个类,如 Channel1、Channel2 ...。这是我的频道类:

public class Channel {

    @SerializedName("song")
    @Expose
    private String song;
    @SerializedName("artist")
    @Expose
    private String artist;
    @SerializedName("duration")
    @Expose
    private String duration;
    @SerializedName("playedat")
    @Expose
    private String playedAt;
    @SerializedName("image_extralarge")
    @Expose
    private String img;
    //Getters, setters etc.
}

那么,在我的情况下我应该如何解析这个 JSON 呢?

标签: javaandroidretrofit2pojo

解决方案


您的问题出在您的 json 中,它应该如下所示:

[  
 {  
     "song":"Crush",
     "artist":"Jennifer Paige",
     "duration":"180",
     "playedat":"1545065265",
     "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/8eecc92227fcbb09b43472f000df74e1.png"
 },
 {  
     "song":"Reasons Why",
     "artist":"Brand New Immortals",
     "duration":"180",
     "playedat":"1545065371",
     "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/c059afda95dd35354af26cf72e5deab4.png"
 },
 {  
     "song":"Dance Me To The End Of Love",
     "artist":"Leonard Cohen",
     "duration":"300",
     "playedat":"1545065181",
     "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/a368617dc7dc4716a9badb523ff6e7d4.png"
 }
]

因为您拥有的 json 不被视为 List,所以它是一个 json 对象,它有多个 json 对象,每个对象都有一个键,可以将其读取为Map<String, Object>不是列表。


推荐阅读