【发布时间】:2019-02-24 14:11:40
【问题描述】:
所以我已经在 Postman 中尝试了这个请求,它工作正常。但是当我在 Android 中这样做时, response.body() 是空的。该请求发送一个用于登录的电子邮件和密码,并应返回带有其他详细信息的用户对象。
loginUser.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) {
loginView.onLoginResult(response.body());
} else {
loginView.showLoginErrorResult();
}
}
这是我的改造界面:
public interface NetworkAPI {
@POST("/login")
Call<User> login(@Body UserLogin userLogin);
}
改造客户端:
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getAPI(String BASE_URL) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(logging).build();
if (retrofit == null) {
retrofit = new Retrofit
.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
我的用户类:
public class User {
@SerializedName("id")
@Expose
private String id;
@SerializedName("token")
@Expose
private String token;
@SerializedName("email")
@Expose
private String email;
@SerializedName("firstName")
@Expose
private String firstName;
@SerializedName("lastName")
@Expose
private String lastName;
public User(String id, String token, String email, String firstName, String lastName) {
this.id = id;
this.token = token;
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
}
//getters &setters...
}
邮递员请求:
{
"email":"test@email.com",
"password":"123456"
}
邮递员回复:
{
"success": true,
"payload": {
"email": "test@email.com",
"firstName": "test",
"lastName": "test",
}
}
我也收到 200 条响应,但没有关于正文的数据
【问题讨论】:
-
请edit 并发布Retrofit 接口类、示例URL 和Postman 客户端截图。
-
分享您的用户类和来自 Postman 的 Json 响应..
-
@IntsabHaider 我更新了我的帖子
标签: android api retrofit2 response