【问题标题】:How to handle empty array return in API response in retrofit 2 android?如何在改造 2 android 中处理 API 响应中的空数组返回?
【发布时间】:2019-11-05 03:18:04
【问题描述】:

My API 的响应如下。

{
"status": true,
  "message": "",
  "data": {
    "property_data": [
      {
        "iPropertyID": "179",
        "sImage": "http://192.168.1.100:8080/protechdna/img/property/180219045755Etim%25web.jpg",
        "isImageUploaded": true,
        "sTypeOfPropertyMarked": "Sound System",
        "sModel": "201",
        "sMake": "sony",
        "sSerialNumber": "12456789",
        "sApproxValue": "149",
        "sDescriptionOfProperty": "Description Of Property Description Of Property",
        "sUniqueidentifyingFeatures": "Unique identifying features Unique identifying features Unique identifying features",
        "room_detail": {
          "iRoomID": "242",
          "sRoomName": "romania"
        },
        "pin_detail": {
          "iPinID": "788",
          "sPin": "12345",
          "kit_detail": {
            "iKitID": "6",
            "sTypeOfKit": "DNA Spray"
          }
        },
        "owner_detail": {
          "iUserID": "702",
          "sEmailID": "priyank.nyusoft@gmail.com",
          "full_name": "priyank Patel",
          "address_line": "Main street",
          "sPhone": "(999) 999-9999",
          "state_detail": {
            "iStateID": "41",
            "sStateName": "Ohio"
          },
          "county_detail": {
            "iCountyID": "2117",
            "sCountyName": "Portage"
          },
          "city_detail": {
            "iCityID": "20241",
            "sCityName": "Newton Falls"
          },
          "zipcode_detail": {
            "iZipCodeID": "18762",
            "sZipcode": "44444"
          }
        }
      }
    ],
    "bicycle_data": [
      {
        "iBicycleID": "357",
        "sImage": "http://192.168.1.100:8080/protechdna/img/property/220419021553101018213027hi%20res%20bike.jpg",
        "isImageUploaded": true,
        "sBicycleName": "catrike",
        "sModel": "fgfdg",
        "sMake": "dfgfg",
        "sSerialNumber": "gfhfghtgf",
        "sApproxValue": "456",
        "sBicycleColor": "gff",
        "sUniqueidentifyingFeatures": "fghfh",
        "bicycletype_detail": {
          "iBicycleTypeID": "3",
          "sTypeName": "Touring Bikes"
        },
        "pin_detail": {
          "iPinID": "788",
          "sPin": "12345",
          "kit_detail": {
            "iKitID": "6",
            "sTypeOfKit": "DNA Spray"
          }
        },
        "owner_detail": {
          "sEmailID": "bharat.nyusoft@gmail.com",
          "full_name": "golden bicycle New",
          "address_line": "t",
          "iUserID": "",
          "sPhone": "",
          "state_detail": {
            "iStateID": "12",
            "sStateName": "Florida"
          },
          "county_detail": {
            "iCountyID": "357",
            "sCountyName": "Leon"
          },
          "city_detail": {
            "iCityID": "3900",
            "sCityName": "Tallahassee"
          },
          "zipcode_detail": {
            "iZipCodeID": "13609",
            "sZipcode": "32312"
          }
        }
      },
      {
        "iBicycleID": "356",
        "sImage": "http://192.168.1.100:8080/protechdna/img/property/020419084735041018112608hi%20res%20bike.jpg",
        "isImageUploaded": true,
        "sBicycleName": "catrike",
        "sModel": "erer 123",
        "sMake": "df",
        "sSerialNumber": "12456789012",
        "sApproxValue": "10",
        "sBicycleColor": "erer",
        "sUniqueidentifyingFeatures": "erer",
        "bicycletype_detail": {
          "iBicycleTypeID": "2",
          "sTypeName": "Cyclocross Bikes"
        },
        "pin_detail": {
          "iPinID": "788",
          "sPin": "12345",
          "kit_detail": {
            "iKitID": "6",
            "sTypeOfKit": "DNA Spray"
          }
        },
        "owner_detail": {
          "iUserID": "600",
          "sEmailID": "das@gmail.com",
          "full_name": "Das Patel",
          "address_line": "",
          "sPhone": "(123) 777-7007",
          "state_detail": null,
          "county_detail": null,
          "city_detail": null,
          "zipcode_detail": null
        }
      }
    ],
    "vehicle_data": [

    ],
    "accessory_data": [

    ]
  }
}

在最后一个数组中,您可以看到车辆数据和附件数据中没有数据并显示括号 当 API 返回这种响应时,改造响应将在 onfailure 中代替 onResponse。 任何人都可以帮助我吗? 这是一个第 3 方 API,我无法修改来自服务器端的响应。

【问题讨论】:

  • pojo 本身一定有问题。你能在这里添加你的 pojo/model 类吗?
  • @Napolean 感谢您的支持,当我检查具有数据和空白数组的结果时发现当我获取数据时他们正在发送 JSON 对象,而当没有数据时他们正在发送 JSON 数组这就是为什么 Model 类没有解析它,结果是失败而不是成功。

标签: android retrofit2


【解决方案1】:

在这种情况下,您将返回一个没有属性类型/数据类型的列表,让我清楚如果您有属性/项目,您可以创建一个 pojo 类来捕获响应,那么您认为如何捕获响应没有模型/pojo 类型类,因此这是导致 OnFailour 方法的异常。 如果您想使用此列表,请将一些属性放在那里并将它们设置为 null,就像您对其他列表所做的那样

通过其他方式,您可以确定在成功响应后是否为空,将此条件放入您的 OnResponse 方法中

if(response.body().toString().length()==0)
  {
    //array is empty
  }

【讨论】:

  • 这是一个返回值的第 3 方 API,所以我无法修改 API 的响应。
  • 我编辑了我的答案并添加了一些额外的信息检查它,如果你实现它,请告诉我
猜你喜欢
  • 2019-06-20
  • 1970-01-01
  • 2018-07-07
  • 2013-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-15
  • 1970-01-01
相关资源
最近更新 更多