java - Retrofit 2 请求返回空对象引用

标签 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...。这是我的 channel 类(class):

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中,它应该看起来像:

[  
 {  
     "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>不是列表。

关于java - Retrofit 2 请求返回空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53821470/

相关文章:

java - 从 retrofit 查询中获取 map

java - 在 SQLite 中使用 IN 子句

android - 如何将 TextView 对齐到 ImageView 的右侧

java reader classloader/导出后不起作用

java - 重载Android应用程序?

java - 以编程方式更改现有形状中的纯色

android - Retrofit response : Cache Control. 是什么意思?

java - 像在 jQuery ajax 中一样使用 Retrofit 发出 POST Json 请求

Java 转批处理文件

Java温度转换器错误