没有得到正确的json响应作为预期的实际响应

时间:2018-04-17 10:27:37

标签: java android json retrofit2

这是我的代码实际响应和预期的响应完全不同。任何人都可以追踪我的错误,提前谢谢。

主要活动

        final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        ApiInterface apiService =
                ApiClient.getClient().create(ApiInterface.class);

        Call<List<UserModel>> call = apiService.getUserInfo();

        call.enqueue(new Callback<List<UserModel>>() {
            @Override
            public void onResponse(Call<List<UserModel>> call, Response<List<UserModel>> response) {
                int statusCode = response.code();
                List<UserModel> rs = response.body();
                Log.i("xxxxxxggggxxxxxxxxxxx",rs+"");

            }

            @Override
            public void onFailure(Call<List<UserModel>> call, Throwable t) {

            }
        });





usermodel 


    public class UserModel {

        @SerializedName("profile")
        @Expose
        private Profile profile;

        public Profile getProfile() {
            return profile;
        }

        public void setProfile(Profile profile) {
            this.profile = profile;
        }

    }

profile  

    public class Profile {


        @SerializedName("isActive")
        @Expose
        private Boolean isActive;
        @SerializedName("name")
        @Expose
        private Name name;




        public Boolean getIsActive() {
            return isActive;
        }

        public void setIsActive(Boolean isActive) {
            this.isActive = isActive;
        }

        public Name getName() {
            return name;
        }

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

    }

名称

public class Name {

    @SerializedName("first")
    @Expose
    private String first;
    @SerializedName("last")
    @Expose
    private String last;

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public String getLast() {
        return last;
    }

    public void setLast(String last) {
        this.last = last;
    }

}

ApiCLIENT

public interface ApiInterface {
    @POST("test")
    Call<List<UserModel>> getUserInfo();

}

其实际回应是

[{"profile":{"id":"d712923e-62a1-11e5-9d70-feff819cdc9f","email":"trevor@ribot.co.uk","avatar":"https://s3-eu-west-1.amazonaws.com/api.ribot.io/trevor_big.png","dateOfBirth":"1990-01-01T00:00:00.000Z","hexColor":"#B60042","bio":"Trevor crafts elegant multi-device experiences with pixels and magic while tanked up on artisan coffee and Col-Erase pencil shaving fumes. He can also be found going on about Disneyland (again), playing video games terribly, or drawing a face on something.","isActive":true,"name":{"first":"Trevor","last":"May"}}}]

这是我收到的回复info.androidhive.retrofit.model.UserModel@9c6c34c

这是我的代码实际响应和预期的响应完全不同。任何人都可以追踪我的错误,提前谢谢。

3 个答案:

答案 0 :(得分:0)

  

这是我收到的回复info.androidhive.retrofit.model.UserModel@9c6c34c

当然,当您尝试打印回复正文时,您将拥有此类型的日志,因为您正在尝试打印对象列表,即List<UserModel>

因此,如果你想检查你收到的东西,你可以查看计数,如

Log.i("xxxxxxggggxxxxxxxxxxx",""+rs.size());

答案 1 :(得分:0)

从您的代码中,您应该做的第一件事是在 UserModel 类中实现 toString 方法来调试很容易,它会返回 profile.email ,例如。

然后在 onResponse 方法中,您应该使用for循环来遍历List并调用 toString 每个项目:

for (UserModel userModel: rs) {
   Log.d("DEBUG_TAG", userModel.toString());
}

答案 2 :(得分:0)

如果您需要JSON对象,则应在界面中添加@Headers标记。

e.g。

@Headers("Content-Type: application/json")
@GET("Allusers/{iD}")
Call<String> doesUserExists(@Path("iD") String iD);

此外,您必须解析响应或将其转换为您可以操作的对象。

@Override
public void onResponse(Call<String> call, Response<String> response) {
    if(response.body() != null){
        JsonParser parser = new JsonParser();
        JsonElement element = parser.parse(response.body());
        if(!element.isJsonNull()){
            JsonObject obj;
            try{
                obj = element.getAsJsonObject();
                JsonElement elementA = obj.get("variableName");
                JsonElement elementB = obj.get("varialeName");

                int convertedElementA = elementA.getAsInt();
                int convertedelementB  = elementB.getAsInt();

            }catch (Exception ee){
                LOG.info("...");
            }

        }
    }
}