首页 > 解决方案 > 如何在改造 2 android 中处理 API 响应中的空数组返回?

问题描述

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,我无法修改来自服务器端的响应。

标签: androidretrofit2

解决方案


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

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

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

推荐阅读