【问题标题】: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)如何处理改造的响应(这里我的响应没有显示数据,它只显示调用的代码和状态)
【发布时间】:2021-01-23 03:40:57
【问题描述】:

我正在使用改造从我的 android 应用程序执行 api 调用。但响应显示状态代码 200 和 ok 消息。 但是调用的数据是由 httpClient 返回的。 那么如何处理我的呼叫的响应数据。 这里的响应将是

请求负载

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

回复:

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

这是我打印出来的回复,不会给出上述数据。如何将令牌设置为我的下一个呼叫?

response ==== Response{protocol=http/1.1, code=200, message=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从response.getBody();获取token
  • 能否告诉我 getBody 来自哪些依赖项。我只得到 response.body() 它返回 null
  • 哦,对不起,是response.body()。提供您的用户类代码。
  • @ErwinKurniawanA 将 User.java 修改为 LoginRequest.java。你能看看上面的代码吗?现在得到响应为 mypackagename.LoginRequest@edfafe9
  • 通过getData()访问,应该是response.body().getData()

标签: java android-studio retrofit2


【解决方案1】:

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

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

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

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

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

因此,您需要一个类来获得对它的价值的可访问性。比如说User class。

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

}

最后,处理改造调用的类响应。假设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

}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多