我如何处理改造的响应(这里我的响应没有显示数据,它只显示调用的代码和状态)

How can I handle the response from the retrofit (Here my response not showing the data, it only shows the code and status of the call)

我正在使用改造从我的 android 应用程序进行 api 调用。但是响应显示状态代码 200 和 ok 消息。 但是调用的数据是httpClient return。 那么我该如何处理我的电话的响应数据。 这里的响应将是

请求负载

/okhttp.OkHttpClient: {"data":{"email":"foobar@gmail.com","password":"PASSWoRD121"}}

回复:

okhttp.OkHttpClient: {"data":"my tokken"}

这里是我打印的回复,不会给出以上数据。如何为我的下一个电话设置令牌?

响应 ==== 响应{协议=http/1.1,代码=200,消息=OK,url="http://tyhgfhfty/hfhgfh /"}

ApiService.java

 @POST(ApiConstant.Login)
 Call<User> LoginRequest(@Body JsonObject user);

登录活动:

ApiService userLoginService = retrofit.create(ApiService.class);
        final JsonObject jo = new JsonObject();

        jo.addProperty(ApiParameter.EMAIL, email);
        jo.addProperty(ApiParameter.PASSWORD, password);
        final JsonObject jobj = new JsonObject();
        jobj.add(ApiParameter.DATA, jo);
        userLoginService.userLogin(jobj).enqueue(new Callback<LoginRequest>(){
            @Override
            public void onResponse(Call<LoginRequest> call, Response<LoginRequest>response) {
                System.out.println(("response ===" + response));

LoginRequest.java

public class LoginRequest {

private String email;
private String password;

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

public void setPassword(String password) {
    this.password = password;
}

public String getEmail() {
    return email;
}

public String getPassword() {
    return password;
}

}

当你有一个json响应时,你可以分析或假设一个json等于一个class,因为Gson转换。

其中 json 包含一个键 data 和一个字符串 my tokken

在 class 改造响应中,它是名为 data 的相等变量,它来自类型为 String 的键 data,为什么是字符串?因为 value 我的 tokken 是 json 中的一个字符串。因此您稍后可以从 data getter setter 检索该值。喜欢getData();

因此对于 {"data":"my tokken"},您的 LoginResponse class 仅包含一个 data 类型 String 和 setter getter.

当您有回复时 {"data": {"user": "xxxx", "email": "foobar@gmail.com", "lastname": "yyyyy", "gender": 1, "deviceType": 1}"}。你可以分析出keydata包含一个json对象; 一个json等于一个class.

因此,您需要 class 才能获得它的价值。这么说吧Userclass.

public class User {

     private String user; // because the value of user in json is String
     private String email;
     private String lastname;
     private Int gender; // because the value of gender in json is Int
     private Int deviceType;

     // the setter getter here

}

最后,您的 class 响应处理改造调用。假设 UserResponse 应该是这样的

public class UserResponse {

     private User data; 
     // the variable is named data because it should be the same to the json key and the type of the variable is class `User`. Remember about the bolded text
     // (variable named same is not a must, if different, you can use `SerializedName` annotation, you can read about it later) 

     // the setter getter here

}

我用简单的方式解释了我的想法,希望你能理解。