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

时间:2019-06-17 04:47:17

标签: 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界面:

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

resultList.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

2 个答案:

答案 0 :(得分:0)

我认为它们是您的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);
}

希望这会有所帮助。让我知道他们是否对此有任何疑问!

编码愉快!

答案 1 :(得分:0)

我认为您的回应pojo课堂上有问题。 pojo类应该是一个带有jsonarray的对象,下面是一个粗糙的代码。

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

#define MAX_CONNECTIONS 10
#define BUF_SIZE        2000
#define STREQU(a,b)     (strcmp(a, b) == 0)

void init_local_sockaddr_in(struct sockaddr_in *sock, int portnum) {
    sock->sin_family = AF_INET;
    sock->sin_addr.s_addr = inet_addr("127.0.0.1");
    sock->sin_port = htons(portnum);
}

int main(int argc, char *argv[])
{
    int listen_sockfd, input_sockfd, c, read_size, n;
    struct sockaddr_in server, client;
    char client_message[2000] = {0};
    char bufferout[2000] = {0};

    listen_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (listen_sockfd == -1) {
        printf("Could not create sockets");
    }
    puts("Socket created");

    // Just for better debugging
    int optval = 1;
    setsockopt(listen_sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int));

    init_local_sockaddr_in(&server, 10025);
    n = bind(listen_sockfd, (struct sockaddr*) &server, sizeof(server));
    if (n < 0) {
        perror("bind failed. Error");
        return 1;
    }

    listen(listen_sockfd , MAX_CONNECTIONS);

    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);

    while(1) {
        input_sockfd = accept(listen_sockfd, (struct sockaddr*) &client, (socklen_t*) &c);
        if (input_sockfd < 0) {
            perror("accept failed");
            return 1;
        }
        puts("Connection accepted");

        sprintf(bufferout, "250 mail.domain.com says hello\r\n"); // I tried different replies here, including 220
        send(input_sockfd, bufferout, strlen(bufferout), 0);
        puts(bufferout);

        int keep_running = 1;

        while (keep_running) {
            n = read(listen_sockfd, client_message, sizeof(client_message));
            if (STREQU(client_message, ".") || n < 0) {
                keep_running = 0;
                break;
            }
            printf("Read %lu bytes: %s\n", strlen(client_message), client_message);
            memset(client_message, 0, sizeof(client_message));
        }
        close(input_sockfd);
        puts("Finished reading current message");
    }
    close(listen_sockfd);

    return 0;
}