Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在尝试检索JSON数组时,REFORNATION2 GET请求返回空列表_Java_Android_Json_Retrofit2 - Fatal编程技术网

Java 在尝试检索JSON数组时,REFORNATION2 GET请求返回空列表

Java 在尝试检索JSON数组时,REFORNATION2 GET请求返回空列表,java,android,json,retrofit2,Java,Android,Json,Retrofit2,我目前正在使用改型2检索对象的JSON数组。与my URL的连接成功,但由于列表始终以null返回,因此从未成功检索数据。我不确定为什么会发生这种情况;非常感谢您的帮助,谢谢 JSON数据: { "result":{ "data":[ { "image":"test1", "text":"text1", "time":1, "key":"pm-02" }, { "

我目前正在使用改型2检索对象的JSON数组。与my 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接口:

公共接口Api{
字符串BASE_URL=“URL”;
@获取(“路径TourlFunction”)
CallgetResult(@Query(“uid”)
字符串myUID,@Query(“金额”)字符串
金额);
}
resultList.java

公共类结果列表{
私有静态最终字符串TAG=“结果列表”;
@SerializedName(“数据”)
@暴露
私人名单数据;
公共列表getResult(){
d(标记“堆栈溢出-在我的结果列表类中”+数据);
返回数据;
}
public void setResult(List temp){this.data=temp;}
}
MessageReference.java

公共类MessageReference{
私有字符串图像;
私有字符串文本;
私人整数时间;
私钥;
公共消息引用(字符串图像、字符串文本、整数时间、字符串键){
this.text=文本;
这个图像=图像;
这个时间=时间;
this.key=key;
}
public MessageReference(){}
公共字符串getImage(){返回图像;
}
公共字符串getText(){return text;}
public int getTime(){return time;}
公共字符串getKey(){return key;}
public void setImage(字符串图像){this.image=image;}
public void setText(字符串文本){this.text=text;}
public void setTime(int time){this.time=time;}
public void setKey(字符串键){this.key=key;}
}
Java(调用的位置)

reformation-reformation=new-reformation.Builder().baseUrl(Api.BASE\u URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
...
Api=改装.create(Api.class);
Log.d(标记“堆栈溢出-API创建”+API);
//get result接受2个参数-用户的uid(用于检索其数据),以及返回的json数组中所需的对象数量,在本例中为-5
Call Call=api.getResult(loggedinUID,“5”);
Log.d(标记“堆栈溢出-调用创建”+调用);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
日志d(标签“响应时”+呼叫);
resultList result=response.body();
i(标记“堆栈溢出-我的调用”+result.getResult());
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.i(标签“故障”);
t、 printStackTrace();
}
});
}
LogCat(在上面的文件中)


我认为他们的问题在你的结果列表课上。在模型类中使用“数据”而不是调用“结果”

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

您的模型类应如下所示:

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

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;
}

}
您的API接口应该如下所示:

public interface Api {
  String BASE_URL = "URL";
  @GET("PathtoUrlFunction")
  Call<Result>getResult(@Query("uid") 
  String myUID, @Query("amount") String 
  amount);
}
公共接口Api{
字符串BASE_URL=“URL”;
@获取(“路径TourlFunction”)
CallgetResult(@Query(“uid”)
字符串myUID,@Query(“金额”)字符串
金额);
}
希望这会有所帮助。让我知道他们是否对此有任何问题


快乐编码

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

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

您的模型类应如下所示:

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

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;
}

}
您的API接口应该如下所示:

public interface Api {
  String BASE_URL = "URL";
  @GET("PathtoUrlFunction")
  Call<Result>getResult(@Query("uid") 
  String myUID, @Query("amount") String 
  amount);
}
公共接口Api{
字符串BASE_URL=“URL”;
@获取(“路径TourlFunction”)
CallgetResult(@Query(“uid”)
字符串myUID,@Query(“金额”)字符串
金额);
}
希望这会有所帮助。让我知道他们是否对此有任何问题


快乐编码

我认为你在pojo课上的回答有问题。pojo类应该是一个对象,里面有一个对象,带有jsonarray

public class ApiResponse {

    @SerializedName("result")
    Result result;
}

public class Result{

    @SerializedName("data")
    List<MessageReference> list;

}

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

公共类响应{
@SerializedName(“结果”)
结果;
}
公开课成绩{
@SerializedName(“数据”)
名单;
}
公共类消息引用{
字符串图像;
字符串文本;
整数时间;
字符串键;
}

我认为您的回答pojo类中存在问题。pojo类应该是一个对象,里面有一个对象,带有jsonarray

public class ApiResponse {

    @SerializedName("result")
    Result result;
}

public class Result{

    @SerializedName("data")
    List<MessageReference> list;

}

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

公共类响应{
@SerializedName(“结果”)
结果;
}
公开课成绩{
@SerializedName(“数据”)
名单;
}
公共类消息引用{
字符串图像;
字符串文本;
整数时间;
字符串键;
}

请使用Postmanth检查API。API与邮递员一起检查;它返回与我期望的相同的JSON数据(我在问题中包括了这些数据。)@basisplease使用postmanthe检查API API与Postman签出;它返回与我期望的相同的JSON数据(我在问题中包含了这些数据。)@basi如果您发现这是正确的答案,可以将其标记为正确!如果您发现这是正确的答案,您可以将其标记为正确!