英文:
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)
问题
我正在使用 Retrofit 在我的 Android 应用中进行 API 调用。但是响应显示状态码为 200,带有 OK 消息。
但是调用的数据是由 httpClient 返回的。
那么我如何处理调用的响应数据呢?
这里的响应内容将会是
请求载荷
**/okhttp.OkHttpClient: {"data":{"email":"foobar@gmail.com","password":"PASSWoRD121"}}**
响应:
**okhttp.OkHttpClient: {"data":"我的令牌"}**
这里打印的响应不会给出上面的数据。我如何将令牌设置给我的下一次调用呢?
**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;
}
}
英文:
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;
}
}
答案1
得分: 1
当您有一个JSON响应时,您可以分析或假设一个JSON等于一个类,因为Gson进行了转换。
在该JSON中包含一个键data
和一个字符串my tokken
。
在类retrofit响应中,它等于名为data
的变量,该变量来自键data
,类型为String
,为什么是String?因为该JSON中的值my tokken是一个字符串。所以您可以稍后从data
的getter setter中检索该值。像getData();
所以对于{"data":"my tokken"}
,您的LoginResponse
类只包含一个字段,即类型为String
的data
,以及setter getter。
当您获得响应{"data":{"user":"xxxx","email":"foobar@gmail.com","lastname":"yyyyy","gender":1,"deviceType":1}}
。您可以分析键data
包含一个JSON对象;一个JSON等于一个类。
所以,您需要一个类来访问它的值。假设是User
类。
public class User {
private String user; //因为JSON中user的值是String
private String email;
private String lastname;
private Int gender; //因为JSON中gender的值是Int
private Int deviceType;
//这里是setter getter
}
最后,处理retrofit调用的类响应。假设UserResponse
应该像这样
public class UserResponse {
private User data;
//变量命名为data,因为它应该与JSON键相同,变量的类型是类`User`。请记住加粗的文字
//(变量命名相同不是必须的,如果不同,您可以使用`SerializedName`注解,以后可以阅读相关内容)
//这里是setter getter
}
我以简单的方式解释了我的思路,希望您能理解。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论