当我使用Retrofit来调用Login API时,我遇到一个小问题:响应主体为null。响应消息包含以下消息:
“响应{protocol = http / 1.1,code = 200,消息= OK,url = http://gagron.com/api/login.php}”
类接口
public interface getLoginDataService {
public String BaseURL = Constants.mBase_Url;
@FormUrlEncoded
@POST(Constants.mLogin)
Call<UserModel> login(@Field("email") String email, @Field("password") String password);
}
登录方法
public void loginConnector(String email, String password) {
Retrofit retrofit = new Retrofit.Builder().baseUrl(Connectors.getLoginDataService.BaseURL)
.addConverterFactory(GsonConverterFactory.create(new Gson())).build();
Connectors.getLoginDataService getLoginDataService = retrofit.create(Connectors.getLoginDataService.class);
getLoginDataService.login(email, password).enqueue(new Callback<UserModel>() {
@Override
public void onResponse(Call<UserModel> call, Response<UserModel> response) {
UserModel model= response.body();
Log.i("Successmsg", "" + response.toString());
Log.i("Successmsg1", "" + model.getFirstName());
;
}
@Override
public void onFailure(Call<UserModel> call, Throwable t) {
Log.i("Errormsg", t.getMessage() + "");
}
});
}
最后是考虑响应的用户模型。
Class UserModel
public class UserModel {
@SerializedName("FirstName")
@Expose
private String firstName;
@SerializedName("LastName")
@Expose
private String lastName;
@SerializedName("Email")
@Expose
private String email;
@SerializedName("Mobile")
@Expose
private String mobile;
@SerializedName("Gender")
@Expose
private String gender;
@SerializedName("Password")
@Expose
private String password;
@SerializedName("Salt")
@Expose
private String salt;
@SerializedName("Address")
@Expose
private String address;
@SerializedName("PostalCode")
@Expose
private String postalCode;
@SerializedName("DateOfBirth")
@Expose
private String dateOfBirth;
@SerializedName("role")
@Expose
private String role;
@SerializedName("newsletter")
@Expose
private String newsletter;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getNewsletter() {
return newsletter;
}
public void setNewsletter(String newsletter) {
this.newsletter = newsletter;
}
}
答案 0 :(得分:2)
在您的loginConnector
方法中,您可以使用call.request().url()
显示/调试您正在调用的请求URL。
此外,您可以使用REST客户端向该URL发出POST请求,并检查两个响应之间的差异。如今,Insomnia REST client是一个不错的选择。
希望对您有所帮助。
答案 1 :(得分:0)
在您的onResponse
添加中
if (response.isSuccessful()) {
if (response.body() != null) {UserModel model= response.body();
Log.i("Successmsg", "" + response.toString());
Log.i("Successmsg1", "" + model.getFirstName());
}
} else {
Toast.makeText(LoginSM.this, getString(R.string.wrongdata), Toast.LENGTH_SHORT).show();
}