android - 如何处理改造中的两种不同响应

标签 android json retrofit2 jsonschema2pojo

我关注了this使用 Retrofit2 POST 数据

我用过JSON POJO解析我的 POST 和 GET 文件

所以如果记录中的数据存在,我会得到这种响应

{
"status": "200",
"response": [{
        "cnt_id": "201",
        "phn_no": "3251151515",
        "dat_cnt": "Reset Password request Said to Mail"
    },
    {
        "cnt_id": "209",
        "phn_no": "555465484684",
        "dat_cnt": "Hi DEMO User , Congratulations! Your account has been created successfully."
    },
    {
        "cnt_id": "210",
        "phn_no": "4774748",
        "dat_cnt": "Hi XYZ , Congratulations! Your account has been created successfully."
    }
]
}

如果没有数据我会得到

{"status":"204","re​​sponse":{"msg":"无内容"}}
{"status":"400","re​​sponse":{"msg":"BadRequest"}}
{"status":"401","re​​sponse":{"msg":"未经授权的用户"}}

所以在这里我可以解析状态 200 中的数据,但是当状态不等于 200 时我想处理它们

我尝试过

   status = response.body().getStatus();
   if(status.equals("200")) {
      List<Response> resList =  response.body(). getResponse();

            for(int i = 0; i<resList.size(); i++)
            {.
             .
              ..
             .
            }
        }

        else {
             //not Implemented
             }

现在我应该写什么,否则我在 POJO 中使用了响应数据,它不等于 200,但我要求列表

更新

com.example.Example.java

public class Example {
    @SerializedName("status") @Expose private String status;
    @SerializedName("response") @Expose private List<Response> response = null;
}        

com.example.Response.java

public class Response {
    @SerializedName("cnt_id") @Expose private String cntId;
    @SerializedName("phn_no") @Expose private String phnNo;
    @SerializedName("dat_cnt") @Expose private String datCnt;
}

最佳答案

public class Example {
    @SerializedName("status") @Expose private String status;
    @SerializedName("response") @Expose private Object response = null;
}

public class Response {
    @SerializedName("cnt_id") @Expose private String cntId;
    @SerializedName("phn_no") @Expose private String phnNo;
    @SerializedName("dat_cnt") @Expose private String datCnt;
}

public class ResponseError{
    @SerializedName("msg") @Expose private String msg;
}

你的回调方法应该是这样的

new Callback<Example>() {
    @Override
    public void onResponse(Call<Example> call, Response<Example> response) {
       if (response.isSuccessful()){
          Example example = response.body();
          Gson gson = new GsonBuilder().create();

          if (example.status.equals("200")) {
              TypeToken<List<Response>> responseTypeToken = new TypeToken<List<Response>>() {};
              List<Response> responseList = gson.fromJson(gson.toJson(example.getResponse()), responseTypeToken.getType());
          } else {
              //If for everyOther Status the response is Object of ResponseError which contains msg.
              ResponseError responseError = gson.fromJson(gson.toJson(example.getResponse()), ResponseError.class);
          }
       }
    }
    
    @Override
    public void onFailure(Call<Example> call, Throwable t) {
        //Failure message
    }
}

关于android - 如何处理改造中的两种不同响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51238140/

相关文章:

php - 无法进入 getJSON 函数()

jquery - $.getJSON 在本地工作,但在上传到 Web 服务器时不起作用

java - 如何使用 Retrofit 发布 ArrayList <Object>?

android - 我如何更改 genymotion 上的默认 gps 位置?

java - 修改函数中的类变量和返回值时出现奇怪的行为

android - 如何克服此错误 :java. lang.OutOfMemoryError:位图大小超出 VM 预算

ios - 创建 API 类来处理 JSON 数据 Swift

java - 可以使用 Retrofit 进行 SOAP web 服务调用吗?

java - 改造 2 @path 与 @query

android - 带有内容提供者的小部件;无法使用 ReadPermission?