首页 > 解决方案 > 我如何处理这两种情况的改造中的数据参数。如果成功为假且数据为空,则应用程序崩溃

问题描述

我的 API 响应:

{
    success: true,
    msg: 'Custom Success Message',
    data : [Objects]
}

如果失败,响应是:

{
    success: false,
    msg: 'Custom Error Message',
    data: null
}

我的问题是如何处理这两种场景的改造数据参数?

如果成功为false且数据为 ,则应用程序崩溃null

标签: androidretrofit2

解决方案


请检查下面的代码以检查获得响应时的空条件。

call1.enqueue(new Callback<YourModelClass>()
{
    @Override
    public void onResponse (Call < YourModelClass > call, Response < YourModelClass > response){

    // this line must be write
    if (Response.body != null) {
         // try this code 
         JSONObject reader = new JSONObject("your response");
         JSONArray data = reader.getString("data"); 

         if(data!=null)
         {

          }
    }
}
}

推荐阅读