我正在使用retrofit2访问webservice API。 API的响应是JSON格式。
JSONResponseImage
在改造中,我的客户端正在成功连接API,但我想访问" statusMessage":"电子邮件地址已经存在"这是由webservice发送的。我尝试了几件事但没有成功。
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) {
Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
} else {
switch(response.code()) {
case 409:
Log.e("TAG", "1: " +response.raw());
Toast.makeText(MainActivity.this, "email already Registered",
Toast.LENGTH_SHORT).show();
break;
}
}
}
seeImageLog
答案 0 :(得分:0)
使用此代码:
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) {
//This is the status message.
String statusMessage = response.body().getStatusMessage();
} else {
switch(response.code()){
case 409:
Log.e("TAG", "1: " +response.raw());
Toast.makeText(MainActivity.this, "email already Registered", Toast.LENGTH_SHORT).show();
break;
}
}
}
User
类:
public class User {
//No need to declare setStatusMessage as the value shall be set by retrofit automatically. No need to make this as public.
private String statusMessage;
public String getStatusMessage() {
//Return statusMessage declared in User class.
return statusMessage;
}
}
答案 1 :(得分:0)
将以下内容添加到User
课程
private Integer statusCode;
private String statusMessage;
public Integer getStatusCode() {
return statusCode;
}
public void setStatusCode(Integer statusCode) {
this.statusCode = statusCode;
}
public String getStatusMessage() {
return statusMessage;
}
public void setStatusMessage(String statusMessage) {
this.statusMessage = statusMessage;
}
现在您可以像getStatusMessage()
一样访问response.body().getStatusMessage();
注意:不需要设置器,添加getter将完成工作