改造2-根据响应类型解析JSON

时间:2018-07-10 07:32:58

标签: android json retrofit retrofit2

我正在使用翻新2,并且我有api,如果有可用的用户详细信息,它将返回用户详细信息对象,否则我将发送一条消息,指出未找到用户详细信息。

 Call<ResponseBody> getCustomerDetail(@Path(value="userDetailId", encoded=true) String userDetailId);

如果我将以上调用称为ResponseBody,那么如果我转换为字符串,则会得到字符串json响应。

 Log.d("detail",response.body().string());

我的问题是我如何将回复转换为我的UserDetail pojo class? 如果我添加Call<UserDetail>,那么我将无法检查是否未找到用户详细信息。所以有什么方法可以实现

我尝试了以下方法,但没有用

 JSONObject obj = new JSONObject(response.body().string());
                Log.d("userdetail",obj.toString());

另一种方法

  Gson gson = new GsonBuilder().create();
              UserDetail userDetail=gson.fromJson(obj.toString(),UserDetail.class );

三种应对方案

{
"customer_id": 138,
"customer_name": "John",
"customer_address": "Usa",
"customer_primary_mobile": "2353253232325",
"customer_secondary_mobile": "325322353232",
"customer_location": null,
"customer_type_id": 14
}

{
"message": "No Records Found"
}

第三

{
"message": "Some think went wrong"
}

3 个答案:

答案 0 :(得分:2)

由于您的响应类型不变(总是给json对象),因此请在json对象中添加所有键

    public class UserDetail {

@SerializedName("message")
@Expose
private String message;
@SerializedName("customer_id")
@Expose
private Integer customerId;
@SerializedName("customer_name")
@Expose
private String customerName;
@SerializedName("customer_address")
@Expose
private String customerAddress;
@SerializedName("customer_primary_mobile")
@Expose
private String customerPrimaryMobile;
@SerializedName("customer_secondary_mobile")
@Expose
private String customerSecondaryMobile;
@SerializedName("customer_location")
@Expose
private Object customerLocation;
@SerializedName("customer_type_id")
@Expose
private Integer customerTypeId;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Integer getCustomerId() {
return customerId;
}

public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}

public String getCustomerName() {
return customerName;
}

public void setCustomerName(String customerName) {
this.customerName = customerName;
}

public String getCustomerAddress() {
return customerAddress;
}

public void setCustomerAddress(String customerAddress) {
this.customerAddress = customerAddress;
}

public String getCustomerPrimaryMobile() {
return customerPrimaryMobile;
}

public void setCustomerPrimaryMobile(String customerPrimaryMobile) {
this.customerPrimaryMobile = customerPrimaryMobile;
}

public String getCustomerSecondaryMobile() {
return customerSecondaryMobile;
}

public void setCustomerSecondaryMobile(String customerSecondaryMobile) {
this.customerSecondaryMobile = customerSecondaryMobile;
}

public Object getCustomerLocation() {
return customerLocation;
}

public void setCustomerLocation(Object customerLocation) {
this.customerLocation = customerLocation;
}

public Integer getCustomerTypeId() {
return customerTypeId;
}

public void setCustomerTypeId(Integer customerTypeId) {
this.customerTypeId = customerTypeId;
}

}

然后在您得到回复的时候

UserDetail user= gson.fromJson("add json reponse here", UserDetail.class);

您可以访问所有字段,例如user.getMessage(), user.getCustomerName()

答案 1 :(得分:1)

更新

为请求创建序列化器。

class UserDetailSerializer{
      @SerializedName("customer_id")
      private Integer customerId;

      ...

      @SerializedName("message")
      private String message;
 }

,然后您可以进行一些检查并获取UserDetail对象或消息。请务必使用Integer而不是int,因为它可能为null。

我认为最好将UserDetailSerializerUserDetail分开,因为多余的message字段会在您的代码中引入一些不一致之处。

我发现充分利用Retrofit2的潜力并直接获取POJO而不自己进行JSON转换是有用的。

旧答案

您可以按照期望包含请求的方式创建POJO,然后从中获取UserDetail。由于它是一个对象,因此可以为null,您可以查看该用户是否存在。

您的对象

class MyResponsePOJO{
    @SerializedName("userdetail")
    UserDetail userDetail;

    ...

    public UserDetail getUserDetail(){
         return this.userDetail;
    }
}

然后是您的Retrofit2请求

MyResponsePOJO getCustomerDetail(@Path(value="userDetailId", encoded=true) String userDetailId);

只需调用getter即可获取

UserDetail userDetail = myResponsePOJO.getUserDetail();

如果您提供的是JSON,我可以给您更精确的答案。

答案 2 :(得分:0)

您可以使用直接json响应获取pojo参考

UserDetail userDetail = new Gson().fromJson(response.body().string(), UserDetail.class);