改装2请求返回空对象引用

时间:2018-12-17 18:58:16

标签: java android retrofit2 pojo

Android新手在这里!我正在尝试读取这样的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?

1 个答案:

答案 0 :(得分:1)

您的问题出在您的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不被视为列表,所以它是json对象,其中包含多个json对象,每个对象都有一个键,可以将其读为Map<String, Object>而不是列表。