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)

huangapple go评论77阅读模式
英文:

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&lt;User&gt; 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&lt;LoginRequest&gt;(){
@Override
public void onResponse(Call&lt;LoginRequest&gt; call, Response&lt;LoginRequest&gt;response) {
System.out.println((&quot;response ===&quot; + 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类只包含一个字段,即类型为Stringdata,以及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 {&quot;data&quot;:&quot;my tokken&quot;}, your LoginResponse class only contain one field that is data with type String and the setter getter.

When you have response {&quot;data&quot;: {&quot;user&quot;: &quot;xxxx&quot;, &quot;email&quot;: &quot;foobar@gmail.com&quot;, &quot;lastname&quot;: &quot;yyyyy&quot;, &quot;gender&quot;: 1, &quot;deviceType&quot;: 1}&quot;}. 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.

huangapple
  • 本文由 发表于 2020年10月8日 14:02:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64256637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定