【问题标题】:Expected BEGIN_ARRAY but was BEGIN_OBJECT in retrofit 2? [duplicate]预期 BEGIN_ARRAY 但 BEGIN_OBJECT 在改造 2 中? [复制]
【发布时间】:2016-12-22 07:51:49
【问题描述】:

我在一个项目中使用改造 2,我无法解析 json 的响应。

Retrofit retrofit = new  Retrofit.Builder().baseUrl("BASE_URL").addConverterFactory(GsonConverterFactory    .create()).build();
final RetrofitApi request = retrofit.create(RetrofitApi.class);
LoginModel loginModel = new LoginModel("user@mail.com",bijoy@123#");
Call<List<LoginResponse>> listCall = request.Loginner(loginModel);
listCall.enqueue(new Callback<List<LoginResponse>>() {
       @Override
       public void onResponse(Call<List<LoginResponse>> call, Response<List<LoginResponse>> response) {
             List<LoginResponse> loginModelList = new ArrayList<>();
             loginModelList = response.body();  

json 响应就像

 {
      "response": {
        "errorCode": "",
        "errorMsg": "",
        "successCode": "SUB001",
        "successMsg": "Login successfully",
        "data": {
          "user_details": {
            "salutation": "Mr.",
            "first_name": "User",
            "last_name": "R",
            "email": "user@mail.com",
            "alternative_email": "",
            "mobile_number": "54312",
            "phone_number": "",
            "title_position": "",
            "department": "",
            "city": "",
            "street": "",
            "state": "",
            "postcode": "",
            "display_name": "Bijoy R",
            "assistant_details": "",
            "country_name": "India",
            "institution_name": "KLO",
            "user_type": "commercial"
          },
          "payment_details": [
            {
              "dateandtime": "28-07-2016 17:4736",
              "payment_mode": "DD",
              "items": "Accompanying Person",
              "transactionId": "",
              "amount": "3000.00",
              "status": "Failed"   }        ],
        }
      }
    } 

如何为这种类型的 json 编写 getter 和 setter。

【问题讨论】:

  • 请显示LoginResponse类
  • 您是否从日志中获得了 json 响应?你的 pojo 课似乎没问题。您需要确保 payment_details 实际上是来自服务器的响应数组。
  • 是的,我得到了成功的响应,但无法解析它,因为改造显示了 BEGIN_ARRAY 但是......

标签: java android json web-services


【解决方案1】:

尝试将Call&lt;List&lt;LoginResponse&gt;&gt; 替换为Call&lt;LoginResponse&gt; 和Response&lt;List&lt;LoginResponse&gt;&gt; 到Response&lt;LoginResponse&gt;

【讨论】:

  • 这个可行,我所有的模型类都是使用 DTO 插件生成的。多谢兄弟! :)
【解决方案2】:

将 Retrofit API 接口返回类型更改为Call&lt;ServerResponse&lt;LoginResponse&gt;&gt;,按照以下代码:

public class ServerResponse<T> {

    private ResponseStatus response;
    private T data;

    public ResponseStatus getResponse() {
        return response;
    }

    public void setResponse(ResponseStatus response) {
        this.response = response;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

public class ResponseStatus {

    private String errorCode;
    private String errorMsg;
    private String successCode;
    private String successMsg;

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public String getSuccessCode() {
        return successCode;
    }

    public void setSuccessCode(String successCode) {
        this.successCode = successCode;
    }

    public String getSuccessMsg() {
        return successMsg;
    }

    public void setSuccessMsg(String successMsg) {
        this.successMsg = successMsg;
    }
}

public class LoginResponse {

    @SerializedName("userDetails")
    private User userDetails;

    @SerializedName("payment_details")
    private List<PaymentDetail> paymentDetails;

    public User getUserDetails() {
        return userDetails;
    }

    public void setUserDetails(User userDetails) {
        this.userDetails = userDetails;
    }

    public List<PaymentDetail> getPaymentDetails() {
        return paymentDetails;
    }

    public void setPaymentDetails(List<PaymentDetail> paymentDetails) {
        this.paymentDetails = paymentDetails;
    }
}

public class User {
    /**
     * Ignore implement
     */
}

public class PaymentDetail {
    /**
     * Ignore implement
     */
}

使用上面的示例来编写与 JSON 响应格式相关的模型类。我建议您的 JSON 响应使用 CamelCase 样式,与 GSON 更好地结合。 对不起,我的英语太有限了。

【讨论】:

    猜你喜欢
    • 2016-06-28
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 2020-03-14
    • 2019-03-04
    • 2014-08-01
    相关资源
    最近更新 更多