android - 如何在改造 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,我无法修改来自服务器端的响应。
解决方案
在这种情况下,您将返回一个没有属性类型/数据类型的列表,让我清楚如果您有属性/项目,您可以创建一个 pojo 类来捕获响应,那么您如何看待没有模型的响应/pojo 类型的类,所以这是一个导致 OnFailour 方法的异常。如果您想使用此列表,请在此处放置一些属性并将它们设置为空,就像您对其他列表所做的那样
以其他方式,您可以确定在成功响应后是否为空,将此条件放入您的OnResponse
方法中
if(response.body().toString().length()==0)
{
//array is empty
}
推荐阅读
- php - 为什么 Ajax 数据表不按当前连接数据库显示数据?
- python - Tensorflow自定义梯度没有给出预期的答案
- angular - 绑定 [禁用] 到属性
- android - 如何从包含异步功能的 firestore 数据库中正确获取结果?- Android - Kotlin - MVVM
- java - 盈透证券 Java API
- windows - 重新加载后如何查看 html 编辑(Firefox DevTools)?
- kubernetes - Kubernetes pod 无法 ping url 或 wget HTTPS 站点
- python - 从数据框中删除子字符串
- reactjs - React - 我输入时表单不断重新渲染组件
- encryption - OpenSSL中aes GCM模式加解密时如何打印key、iv、aad