如何在改造中处理两种不同的响应 [英] How to Handle Two Different Response in Retrofit

查看:53
本文介绍了如何在改造中处理两种不同的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循来使用Retrofit2进行POST数据

I followed this to POST Data Using Retrofit2

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

I Used JSON POJO to Parse my POST and GET files

所以在这里,如果记录中有数据,我将得到这种响应

So Here If Data inside the Records Is there I will get This Kind of Response

{
"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."
    }
]
}

如果没有数据,我会得到

If there is no Data I will get

{"status":"204","response":{"msg":"No Content"}}
{"status":"400","response":{"msg":"BadRequest"}}
{"status":"401","response":{"msg":"Unauthorized User"}}

{"status":"204","response":{"msg":"No Content"}} or
{"status":"400","response":{"msg":"BadRequest"}} or
{"status":"401","response":{"msg":"Unauthorized User"}}

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

So Here I can eable to Parse the data which is there in the status 200 but when Status is not equals to 200 I want to Handle them

我尝试过

   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的响应数据,但我要了它的清单

now what should I write in Else I used response data in POJO which are not equals to 200 but I its asking for list

更新

com.example.Example.java

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

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;
}

您的callBack方法应该类似于

And Your callBack methods should be like

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
            }
        }

这篇关于如何在改造中处理两种不同的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆