【问题标题】:Not getting correct json response as expected actual response没有像预期的实际响应那样得到正确的 json 响应
【发布时间】:2018-04-17 10:27:36
【问题描述】:

这是我的代码中的实际响应和预期响应完全不同。谁能找出我的错误,提前谢谢。

主要活动

        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

这是我的代码中的实际响应和预期响应完全不同。谁能找出我的错误,提前谢谢。

【问题讨论】:

  • 您的预期响应是什么?
  • [{"profile":{"id":"d712923e-62a1-11e5-9d70-feff819cdc9f","email":"trevor@ribot.co.uk","avatar":" s3-eu-west-1.amazonaws.com/api.ribot.io/… 用像素和魔法打造优雅的多设备体验,同时喝着手工咖啡和 Col-Erase 铅笔剃须烟雾。他还可以在迪斯尼乐园(再次)、玩电子游戏或在上面画脸某事。","isActive":true,"name":{"first":"Trevor","last":"May"}}}]

标签: java android json retrofit2


【解决方案1】:

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

当然,当您尝试打印响应正文时,您将获得这种类型的日志,因为您正在尝试打印对象列表,即List&lt;UserModel&gt;

因此,如果您想检查是否收到了一些东西,您可以检查计数,例如

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

【讨论】:

    【解决方案2】:

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

    例如

    @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("...");
                }
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-14
      • 2021-01-14
      • 2018-11-23
      • 2014-04-10
      • 2017-10-27
      • 2021-03-13
      • 2019-07-06
      • 1970-01-01
      相关资源
      最近更新 更多