Reputation: 2644
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":"[email protected]","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;
}
}
Upvotes: 0
Views: 769
Reputation: 1008
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": "[email protected]", "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.
Upvotes: 1