简体   繁体   English

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

[英]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)

I am using retrofit to do the api call from my android app.我正在使用改造从我的 android 应用程序进行 api 调用。 But the response shows the status code 200 with ok message.但是响应显示状态代码 200 和 ok 消息。 But the data for the call is return by httpClient.但是调用的数据是由 httpClient 返回的。 So how can I handle the response data of my call.那么我该如何处理我的呼叫的响应数据。 Here the response will be这里的响应将是

request payload请求有效载荷

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

response:回复:

okhttp.OkHttpClient: {"data":"my tokken"} okhttp.OkHttpClient: {"data":"我的令牌"}

Here is my printed response will not give the above data.这是我打印的回复不会给出上述数据。 How can I set the token to my next calls?如何将令牌设置为下一次调用?

response ==== Response{protocol=http/1.1, code=200, message=OK, url="http://tyhgfhfty/hfhgfh/"}响应====响应{protocol=http/1.1, code=200, message=OK, url="http://tyhgfhfty/hfhgfh/"}

ApiService.java

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

LoginActivity:登录活动:

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登录请求.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;
}

} }

When you have a json response, you can analyze or presume a json is equal a class , because Gson convertion.当你有一个 json 响应时,你可以分析或假设json 等于一个 class ,因为 Gson 转换。

In that json is containing a key data and a string my tokken .在那个 json 中包含一个关键data和一个字符串my tokken

In a class retrofit response it is equal variable named data which is from key data with type String , why String?在类改造响应中,它是相等的名为data变量,它来自类型为String关键data ,为什么是 String ? because value my tokken is a string in that json.因为值我的令牌是该 json 中的一个字符串。 So you can retrieve that value later from data getter setter.因此,您可以稍后从data getter setter 中检索该值。 Like getData();getData();

So for {"data":"my tokken"} , your LoginResponse class only contain one field that is data with type String and the setter getter.因此,对于{"data":"my tokken"} ,您的LoginResponse类仅包含一个字段,该字段是类型为String data和 setter getter。

When you have response {"data": {"user": "xxxx", "email": "foobar@gmail.com", "lastname": "yyyyy", "gender": 1, "deviceType": 1}"} . You can analyze that key data contain a json object; a json equal a class .当你有回应{"data": {"user": "xxxx", "email": "foobar@gmail.com", "lastname": "yyyyy", "gender": 1, "deviceType": 1}"} . 你可以分析包含一个 json 对象的关键data一个 json 等于一个 class

So, you need a class to get accessibility to it value.所以,你需要一个类来获取它的价值。 Let's say it User class.假设它是User类。

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

}

Last, your class response that handle the retrofit call.最后,处理改造调用的类响应。 Let say UserResponse should be like this假设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

}

I explained in simple way of my thinking, i hope you understand about it.我用简单的方式解释了我的想法,希望你能理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM