简体   繁体   English

如何在 retrofit 2 android 响应中处理 API 响应中的空数组返回?

[英]How to handle empty array return in API response in retrofit 2 android?

The response of My API is as below. 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": [

    ]
  }
}

In last array you can see there is no data and showing brackets in vehicle_data and accessory_data When API returns this kind of response the retrofit response is going in onfailure in place of onResponse.在最后一个数组中,您可以看到车辆数据和附件数据中没有数据并显示括号当 API 返回这种响应时,retrofit 响应将在 onfailure 中代替 onResponse。 can anyone help me in this?任何人都可以帮助我吗? This is a 3rd party API which I am not able to modify response from server side.这是第 3 方 API,我无法修改来自服务器端的响应。

In that case you are returning a list with no attributes type/data types,Let me clear If you had attributes/items on it you may make a pojo class to catch the response,so how do you think catch a response what has no model/pojo type class,so this is a exception causeing OnFailour method.在这种情况下,您将返回一个没有属性类型/数据类型的列表,让我明确一下/pojo type class,所以这是一个导致 OnFailour 方法的异常。 If you want to use this List put some attributes there and set them null,like what you did with your other lists如果您想使用此列表,请在此处放置一些属性并将它们设置为 null,就像您对其他列表所做的那样

in other way you can be sure after you get a successful response if it is eampty or not,put this condition in your OnResponse method以其他方式,您可以确定在成功响应后是否为空,将此条件放在您的OnResponse方法中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM