【问题标题】:How to get list of JSON objects from response including other parameter如何从包含其他参数的响应中获取 JSON 对象列表
【发布时间】:2019-10-05 18:43:49
【问题描述】:

我正在继续学习改造并希望处理来自服务器的响应。

Postman 的响应结构

{
    "succeeded": false,
    "errors": [
        {
            "code":"DuplicateUserName",
            "description":"User name 'XXX' is already taken."
        },
        {
            "code": "PasswordTooShort",
            "description": "Passwords must be at least 6 characters."
        },
        {
            "code": "PasswordRequiresLower",
            "description": "Passwords must have at least one lowercase ('a'-'z')."
        },
        {
            "code": "PasswordRequiresUpper",
            "description": "Passwords must have at least one uppercase ('A'-'Z')."
        }
    ]
}

POST 代码

Call<RegisterResponseModel> register(@Body UserJSONModel user);

注册响应模型

    private String succeeded;
    private ArrayList<String> errors;

我尝试使用 List、ArrayList 以及 String 和 evenn 序列化:

@SerializedName("errors")
@Expose

但无论尝试如何,我都会收到一些应该是 success=false 和错误列表的内容

‹      í˝`I–%&/mĘ{JőJ×ŕtˇ€`$Ř�@ěÁ�Íć’ěiG#)«*�ĘeVe]f@Ě흼÷Ţ{ď˝÷Ţ{ď˝÷ş;ťN'÷ß˙?\fdlöÎJÚÉž!€ŞČ?~|?"~ńGÍz:ÍóY>űčŃyV6ů裼®«şůčŃ÷~ńGÓj–ô裧ëUYLł6˙ŞÉëŮ"˙hôŃ,o¦u±j‹jI-đEş¤oŇŹ_eçă´hҬ¬ólvť¶ŮŰ|9ţč—|˙—

【问题讨论】:

    标签: android json rest retrofit2


    【解决方案1】:

    您可以使用 ArrayList 错误解析该 json;

    import java.util.List;
    

    公共类 RegisterResponseModel {

    private boolean succeded;
    private List<Error> errors;
    
    public boolean isSucceded() {
        return succeded;
    }
    
    public void setSucceded(boolean succeded) {
        this.succeded = succeded;
    }
    
    public List<Error> getErrors() {
        return errors;
    }
    
    public void setErrors(List<Error> errors) {
        this.errors = errors;
    }
    
    @Override
    public String toString() {
        return "TestDTO [succeded=" + succeded + ", errors=" + errors + "]";
    }
    

    }

    public class Error {
    
    private String code;
    private String description;
    
    public String getCode() {
        return code;
    }
    
    public void setCode(String code) {
        this.code = code;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    @Override
    public String toString() {
        return "Error [code=" + code + ", description=" + description + "]";
    }
    

    }

    【讨论】:

      【解决方案2】:

      不要像处理成功案例一样处理此案例。如果后端返回错误,您需要像错误一样处理它。 我像这样为 okHttp 使用自定义拦截器

      class ErrorInterceptor : Interceptor {
      
      @Throws(IOException::class)
      override fun intercept(chain: Interceptor.Chain): Response {
          val request = chain.request()
          val response = chain.proceed(request)
          if (<Check is repsonce fail>) {
              val body = response.body()
              body?.let(::parseBackendError) ?: throw IOException("Body is null")
          }
          return response
      }
      
      private fun parseBackendError(responseBody: ResponseBody) {
          <Parce your error list and throw exeption>
        }
      }
      

      【讨论】:

        【解决方案3】:

        将此添加到您的发布方法@FormUrlEncoded

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-10-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多