java - Retrofit2 GET 请求在尝试检索 JSON 数组时返回空列表

标签 java android json retrofit2

我目前正在使用 Retrofit 2 来检索 JSON 对象数组。与我的 URL 的连接成功,但数据从未成功检索,因为列表始终返回为 null。我不确定为什么会发生这种情况;任何帮助将不胜感激,谢谢。

JSON 数据:

{  
"result":{  
  "data":[  
     {  
        "image":"test1",
        "text":"text1",
        "time":1,
        "key":"pm-02"
     },
     {  
        "image":"test2",
        "text":"test2",
        "time":2,
        "key":"pm-03"
     },
     {  
        "image":"test3",
        "text":"test3",
        "time":3,
        "key":"pm-01"
     },
     {  
        "image":"test4",
        "text":"test4",
        "time":4,
        "key":"pm-04"
     },
     {  
        "image":"test5",
        "text":"test5",
        "time":5,
        "key":"pm-07"
     }
  ],
  "nextKey":6
 }
}

API接口(interface):

public interface Api {
  String BASE_URL = "URL";
  @GET("PathtoUrlFunction")
  Call<resultList>getResult(@Query("uid") 
  String myUID, @Query("amount") String 
  amount);
}

结果列表.java

public class resultList {
  private static final String TAG = "RESULT LIST";

  @SerializedName("data")
  @Expose
  private List<MessageReference> data;

  public List<MessageReference> getResult() {
    Log.d(TAG, " STACK OVERFLOW - IN MY RESULT LIST CLASS" + data);
    return data;
}

public void setResult(List<MessageReference> temp) { this.data = temp;}
}

MessageReference.java

public class MessageReference {
  private String image;
  private String text;
  private int time;
  private String key;

  public MessageReference(String image, String text, int time, String key) {
    this.text = text;
    this.image = image;
    this.time = time;
    this.key = key;
 }

  public MessageReference() { }

  public String getImage() { return image; 
}

public String getText() { return text; }

public int getTime() { return time; }

public String getKey() { return key; }

public void setImage(String image) { this.image = image; }

public void setText(String text) { this.text = text; }

public void setTime(int time) { this.time = time; }

public void setKey(String key) { this.key = key; }
}

SocialMediaTab.Java(调用位置)

Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();

...

Api api = retrofit.create(Api.class);
        Log.d(TAG, "STACK OVERFLOW - API CREATE " + api);

        // get result takes in 2 paramters - uid of the user (to retrieve their data), and the amount of objects desired in the returned json array, in this case - 5 
        Call<resultList> call = api.getResult(loggedinUID, "5");
        Log.d(TAG, "STACK OVERFLOW - CALL CREATE " + call);

        call.enqueue(new Callback<resultList>() {
            @Override
            public void onResponse(Call<resultList> call, Response<resultList> response) {
                Log.d(TAG, " ON RESPONSE " + call);
                resultList result = response.body();
                Log.i(TAG, " STACK OVERFLOW - MY CALL" + result.getResult());
            }

            @Override
            public void onFailure(Call <resultList> call, Throwable t) {
                Log.i(TAG, "FAILURE " );
                t.printStackTrace();

            }
        });

    }

LogCat(在上面的文件中)

D/Social Media Tab: STACK OVERFLOW - API CREATE retrofit2.Retrofit$1@131b108
D/Social Media Tab: STACK OVERFLOW - CALL CREATE retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall@d24c09b    
D/RESULT LIST:  STACK OVERFLOW - IN MY RESULT LIST CLASSnull
I/Social Media Tab:  STACK OVERFLOW - MY CALLnull

最佳答案

我认为他们的问题出在你的 resultList 类中。您在模型类中使用“数据”而不是调用“结果”。

预期列表是结果而不是直接数据。

你的模型类应该是这样的:

结果类别

public class Result {

@SerializedName("data") @Expose private List<Datum> data = null; @SerializedName("nextKey") @Expose private Integer nextKey;

public List<Datum> getData() { return data; }

public void setData(List<Datum> data) { this.data = data; }

public Integer getNextKey() { return nextKey; }

public void setNextKey(Integer nextKey) { this.nextKey = nextKey; }

}

基准类别:

public class Datum {

@SerializedName("image")
@Expose
private String image;
@SerializedName("text")
@Expose
private String text;
@SerializedName("time")
@Expose
private Integer time;
@SerializedName("key")
@Expose
private String key;

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public Integer getTime() {
return time;
}

public void setTime(Integer time) {
this.time = time;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

}

示例类:

public class Example {

@SerializedName("result")
@Expose
private Result result;

public Result getResult() {
return result;
}

public void setResult(Result result) {
this.result = result;
}

}

你的 ApiInterface 应该是这样的:

public interface Api {
  String BASE_URL = "URL";
  @GET("PathtoUrlFunction")
  Call<Result>getResult(@Query("uid") 
  String myUID, @Query("amount") String 
  amount);
}

希望这会有所帮助。如果他们对此有任何问题,请告诉我!!

编码愉快!

关于java - Retrofit2 GET 请求在尝试检索 JSON 数组时返回空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56624852/

相关文章:

java - 忽略Maven版本号规则的坏处

java - 如何让 Class.forName 忽略小写/大写

c++ - 如何释放从 native 代码发送回 Java 的 jstring?

android - 我如何将 HTTP 响应从一个 Activity 传递到 android 中的另一个 Activity

android - Android 7.1.1 中的 KeyPairGenerator.generateKeyPair()

javascript - 如何使用 javascript、JSON、python 和 mongodb 之间的日期时间?

java - 可在两个平台上运行的多语言软件

java - 我可以使用 Mockito 延迟 stub 方法响应吗?

json - 如何使用 actions 和 axios.get() (Redux) 从本地 json 文件获取数据?

javascript - 递归操作javascript对象数据结束