【问题标题】:Retrofit 2 request returns null object referenceRetrofit 2 请求返回空对象引用
【发布时间】:2019-05-18 04:31:45
【问题描述】:

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 中的所有这些 Channels 使用了一个 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?

【问题讨论】:

  • 错误很明显。该列表为空。这可能是因为您的 JSON 文件有误 - 要创建一个列表,您应该使用“[ ]”而不是“{ }”来包装所有对象。

标签: java android retrofit2 pojo


【解决方案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不被认为是List,它是一个json对象,有多个json对象,每个对象都有一个key,可以读成Map&lt;String, Object&gt;而不是list。

【讨论】:

    猜你喜欢
    • 2021-02-03
    • 2017-06-19
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    相关资源
    最近更新 更多