简体   繁体   中英

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. But the response shows the status code 200 with ok message. But the data for the call is return by 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"}}

response:

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

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/"}

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

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.

In that json is containing a key data and a string my tokken .

In a class retrofit response it is equal variable named data which is from key data with type String , why String? because value my tokken is a string in that json. So you can retrieve that value later from data getter setter. Like getData();

So for {"data":"my tokken"} , your LoginResponse class only contain one field that is data with type String and the 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 .

So, you need a class to get accessibility to it value. Let's say it 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

}

Last, your class response that handle the retrofit call. Let say UserResponse should be like this

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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