Json的Retrofit 2 return List为空

时间:2018-03-14 20:24:40

标签: java android json gson retrofit2

我现在已经和改造2.3 进行了大约2周的战斗。列表总是对我来说是空的。它只是调用并获取JSON信息,但它不会处理列表。

杰森看起来像这样:

{
"users": [
    {
        "id": 2,
        "name": "Users Name",
        "username": "myusername",
        "facebook_id": null,
        "level": "1",
        "birthdate": "1999-09-09T00:00:00+00:00",
        "email": "user@gmail.com",
        "activated": "",
        "created": "2017-12-07T04:18:30+00:00",
        "answers": [
            {
                "id": 31,
                "question_id": 2,
                "user_id": 2,
                "answer": "School",
                "questions": [
                    {
                        "id": 2,
                        "question": "Where did you meet your best friend?"
                    }
                ]
            },
            {
                "id": 32,
                "question_id": 3,
                "user_id": 2,
                "answer": "Dog",
                "questions": [
                    {
                        "id": 3,
                        "question": "What was your first pet's name?"
                    }
                ]
            }
        ]
    }
],
"message": "Success"
}

Retrofit Interface class:

public interface RestInterface {

String url = "http://myurl.com";

/**
 * Login
 *
 * @param username  Username
 * @param password  Password
 *
 */

@FormUrlEncoded
@Headers("User-Agent:My-Application")
@POST("login")
Call<userlogin> Login(@Field("username") String username,
                      @Field("password") String password);
}

Userlogin类:

public class userlogin {

@SerializedName("users")
@Expose
private List<users> users;
@SerializedName("message")
@Expose
private Object message;

public List<users> getUsers() {
    return users;
}

public void setUsers(List<users> users) {
    this.users = users;
}

public Object getMessage() {
    return message;
}

public void setMessage(Object message) {
    this.message = message;
}

}

用户类:

public class users {

    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("username")
    @Expose
    private String username;
    @SerializedName("facebook_id")
    @Expose
    private String facebookId;
    @SerializedName("level")
    @Expose
    private String level;
    @SerializedName("birthdate")
    @Expose
    private String birthdate;
    @SerializedName("email")
    @Expose
    private String email;
    @SerializedName("activated")
    @Expose
    private String activated;
    @SerializedName("created")
    @Expose
    private String created;


    @SerializedName("answers")
    @Expose
    private List<Answer> answers = null;

    public users(){

    }

    public Integer getId() {
            return id;
    }

    public void setId(Integer id) {
            this.id = id;
    }

    public String getName() {
            return name;
    }

    public void setName(String name) {
            this.name = name;
    }

    public String getUsername() {
            return username;
    }

    public void setUsername(String username) {
            this.username = username;
    }

    public String getFacebookId() {
            return facebookId;
    }

    public void setFacebookId(String facebookId) {
            this.facebookId = facebookId;
    }

    public String getLevel() {
            return level;
    }

    public void setLevel(String level) {
            this.level = level;
    }

    public String getBirthdate() {
            return birthdate;
    }

    public void setBirthdate(String birthdate) {
            this.birthdate = birthdate;
    }

    public String getEmail() {
            return email;
    }

    public void setEmail(String email) {
            this.email = email;
    }

    public String getActivated() {
            return activated;
    }

    public void setActivated(String activated) {
            this.activated = activated;
    }

    public String getCreated() {
            return created;
    }

    public void setCreated(String created) {
            this.created = created;
    }

    public List<Answer> getAnswers() {
            return answers;
    }

    public void setAnswers(List<Answer> answers) {
            this.answers = answers;
    }

}

基本上发生的事情是当它被称为我的“消息”部分回来“成功”时,在我的PHP方面基本上只是声明没有错误。如果有,那么它将返回错误显示。

当尝试获取用户信息时,它总是返回一个空列表。

我的回答总是一样的: 03-14 20:06:26.698 30995-30995 / com.josh.testapp D /回复:{“message”:“成功”,“用户”:[]}

03-14 20:06:26.699 30995-30995 / com.josh.testapp I / System.out:Users :: []

03-14 20:06:26.699 30995-30995 / com.josh.testapp D /消息:成功

我不确定我错过了什么。用户应该作为包含用户信息的列表返回,在这种情况下只是用户登录的信息。但在其他部分,这将显示子用户信息,这就是为什么它在第一个列表形式的地方。

请帮助或指导我朝正确的方向发展。

login.java(调用的地方)

        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RestInterface.url)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();


        RestInterface restInterface = retrofit.create(RestInterface.class);

        Call<userlogin> call = restInterface.Login(
                username.getText().toString(),                            // username
                pass.getText().toString()                                 // password
        );

        call.enqueue(new Callback<userlogin>() {
            @Override
            public void onResponse(Call<userlogin> call, retrofit2.Response<userlogin> response) {

                if (response.isSuccessful()) {
                    userlogin ul = response.body();

                    try{
                        String res = new Gson().toJson(response.body());
                        Log.d("Response", res);
                        System.out.println("Users:: " + ul.getUsers().toString());
                        Log.d("Message", ul.getMessage().toString());

                        List<users> userList = ul.getUsers();

                        for(int i = 0; i < userList.size(); i++){
                            Log.d("Users", userList.get(i).getUsername());
                        }

                    } catch (Exception e){
                        Log.d("exception", e.getMessage());
                    }
                } else {
                    Log.d("unSuccessful", response.message());
                }
            }

            @Override
            public void onFailure(Call<userlogin> call, Throwable t) {
                Log.d("onFailure", t.getMessage());
            }
        });

1 个答案:

答案 0 :(得分:0)

在AbdulAli指出它似乎没有收到用户列表后,我决定查看我的代码并在服务器API上运行一些测试。我发现会话存在问题。他们没有接受,因此返回了一个成功的&#34;但空用户列表。在实现了一些CookieJar函数之后,我能够传递我的cookie用于会话,并且用户列表不再是空的。

虽然我觉得自己是一个因为错过了如此明显的东西的白痴,但我非常感谢你指出AbdulAli。