Android 改装响应
因此,我有一个来自服务器的JSON响应:Android 改装响应,android,retrofit,Android,Retrofit,因此,我有一个来自服务器的JSON响应: { "result": { "id": 30, "status": "Successful." } } 以及一个java类,其中: public class JSONResponse { @SerializedName("result") public JsonObject res; @SerializedName("id") public int id; @Ser
{
"result": {
"id": 30,
"status": "Successful."
}
}
以及一个java类,其中:
public class JSONResponse {
@SerializedName("result")
public JsonObject res;
@SerializedName("id")
public int id;
@SerializedName("status")
public String msg;
}
这里是我称之为服务的地方:
customerResponseCall.enqueue(new Callback<CustomerRequestResponse>() {
@Override
public void onResponse(Call<CustomerRequestResponse> call, Response<CustomerRequestResponse> response) {
response.body().res.get(String.valueOf(response.body().id));
Toast.makeText(MainActivity.this, "User Registed Successfully!!!" + "\n" + "User ID = " + response.body().id, Toast.LENGTH_LONG).show();// this your result
}
@Override
public void onFailure(Call<CustomerRequestResponse> call, Throwable t) {
Log.e("response-failure", call.toString());
}
});
我希望能够在服务器有响应时获取id的值。我该怎么办?请帮助更改您的JSONResponse,如下所示;因为您得到的JSON具有JSONObject结果 结果类 将代码更改为
因为您得到的JSON具有JSONArray结果。结果是JSONObject而不是JSONArray。谢谢,但这不是我想要的。我已经得到了结果,但我特别想得到id的值。我尝试了你的方法,但是它,正如试图得到响应一样。身体我无法获得id。谢谢@piyus:我的错。。但是现在修好了@你添加了我的代码来获取响应了吗?你能发布你的改装客户端代码吗?还有您的CustomerRequestResponse类
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class CustomerRequestResponse{
@SerializedName("result")
@Expose
private Result result;
public Result getResult() {
return result;
}
public void setResult(Result result) {
this.result = result;
}
}
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Result {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("status")
@Expose
private String status;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
customerResponseCall.enqueue(new Callback<CustomerRequestResponse>() {
@Override
public void onResponse(Call<CustomerRequestResponse> call, Response<CustomerRequestResponse> response) {
Integer id = response.body().getResult().getId();
Toast.makeText(MainActivity.this, "User Registered Successfully!!!" + "\n" + "User ID = " + id, Toast.LENGTH_LONG).show();// this your result
}
@Override
public void onFailure(Call<CustomerRequestResponse> call, Throwable t) {
Log.e("response-failure", call.toString());
}
});