今天对接服务器接口获取数据的时候,一直返回获取失败,但不报错.一开始一直以为联网逻辑的问题,但是在联网层打印log是可以拿到返回的数据的,好奇怪的样子,折腾了足足两个小时,来回换了几种联网操作都不行,最后把问题定位在json解析过程中,需要说明一下,我们公司用的是retrofit2框架,数据解析也封装在里面了,但是如果解析失败控制台不打印任何log,这就很坑了,我用as插件gsonformat自动生成的bean,直接上图吧
json串:
{
"flag": "1000",
"userData": [
{
"xh": 3,
"uid": 37,
"background": "http://sy.eytn365.com/1501814586690.jpg",
"frid": 8,
"name": "美女吧",
"headPortrait": "http://sy.eytn365.com/1503060096289.jpg",
"content": "哪张照片进行了虚化效果",
"sid": 30,
"cr": 12.3,
"usetime": 1,
"correctrate": 12.3
}
],
"sData": [
{
"xh": 1,
"uid": 38,
"background": "http://sy.eytn365.com/1501810020283.jpg",
"frid": 8,
"name": "美美",
"headPortrait": "http://sy.eytn365.com/1502957968544.jpg",
"content": "哪张照片进行了虚化效果",
"sid": 30,
"cr": 77,
"usetime": 10,
"correctrate": 12.3
},
{
"xh": 2,
"uid": 36,
"background": "http://sy.eytn365.com/7065230ad9fe0e846f902db49278f057.jpg",
"frid": 8,
"name": "18888888888",
"headPortrait": "http://sy.eytn365.com/u_3910572047,3453464126_fm_26_gp_0.jpg",
"content": "哪张照片进行了虚化效果",
"sid": 30,
"cr": 23,
"usetime": 10,
"correctrate": 23
},
{
"xh": 3,
"uid": 37,
"background": "http://sy.eytn365.com/1501814586690.jpg",
"frid": 8,
"name": "美女吧",
"headPortrait": "http://sy.eytn365.com/1503060096289.jpg",
"content": "哪张照片进行了虚化效果",
"sid": 30,
"cr": 12.3,
"usetime": 1,
"correctrate": 12.3
}
],
"user": {
"id": 38,
"name": "美美",
"phone": "18888888888",
"sex": 1,
"labels": null,
"createTime": 1501739690000,
"headPortrait": "http://sy.eytn365.com/1502957968544.jpg",
"background": "http://sy.eytn365.com/1501810020283.jpg"
}
}
gsonformat自动生成的bean:
public class Bean
{
/**
* flag : 1000
* userData : [{"xh":3,"uid":37,"background":"http://sy.eytn365.com/1501814586690.jpg","frid":8,"name":"美女吧","headPortrait":"http://sy.eytn365.com/1503060096289.jpg","content":"哪张照片进行了虚化效果","sid":30,"cr":12.3,"usetime":1,"correctrate":12.3}]
* sData : [{"xh":1,"uid":38,"background":"http://sy.eytn365.com/1501810020283.jpg","frid":8,"name":"美美","headPortrait":"http://sy.eytn365.com/1502957968544.jpg","content":"哪张照片进行了虚化效果","sid":30,"cr":77,"usetime":10,"correctrate":12.3},{"xh":2,"uid":36,"background":"http://sy.eytn365.com/7065230ad9fe0e846f902db49278f057.jpg","frid":8,"name":"18888888888","headPortrait":"http://sy.eytn365.com/u_3910572047,3453464126_fm_26_gp_0.jpg","content":"哪张照片进行了虚化效果","sid":30,"cr":23,"usetime":10,"correctrate":23},{"xh":3,"uid":37,"background":"http://sy.eytn365.com/1501814586690.jpg","frid":8,"name":"美女吧","headPortrait":"http://sy.eytn365.com/1503060096289.jpg","content":"哪张照片进行了虚化效果","sid":30,"cr":12.3,"usetime":1,"correctrate":12.3}]
* user : {"id":38,"name":"美美","phone":"18888888888","sex":1,"labels":null,"createTime":1501739690000,"headPortrait":"http://sy.eytn365.com/1502957968544.jpg","background":"http://sy.eytn365.com/1501810020283.jpg"}
*/
public String flag;
public UserBean user;
public List<UserDataBean> userData;
public List<SDataBean> sData;
public static class UserBean
{
/**
* id : 38
* name : 美美
* phone : 18888888888
* sex : 1
* labels : null
* createTime : 1501739690000
* headPortrait : http://sy.eytn365.com/1502957968544.jpg
* background : http://sy.eytn365.com/1501810020283.jpg
*/
public int id;
public String name;
public String phone;
public int sex;
public Object labels;
public long createTime;
public String headPortrait;
public String background;
}
public static class UserDataBean
{
/**
* xh : 3
* uid : 37
* background : http://sy.eytn365.com/1501814586690.jpg
* frid : 8
* name : 美女吧
* headPortrait : http://sy.eytn365.com/1503060096289.jpg
* content : 哪张照片进行了虚化效果
* sid : 30
* cr : 12.3
* usetime : 1
* correctrate : 12.3
*/
public int xh;
public int uid;
public String background;
public int frid;
public String name;
public String headPortrait;
public String content;
public int sid;
public double cr;
public int usetime;
public double correctrate;
}
public static class SDataBean
{
/**
* xh : 1
* uid : 38
* background : http://sy.eytn365.com/1501810020283.jpg
* frid : 8
* name : 美美
* headPortrait : http://sy.eytn365.com/1502957968544.jpg
* content : 哪张照片进行了虚化效果
* sid : 30
* cr : 77
* usetime : 10
* correctrate : 12.3
*/
public int xh;
public int uid;
public String background;
public int frid;
public String name;
public String headPortrait;
public String content;
public int sid;
public int cr;//注意这里
public int usetime;
public double correctrate;
}
}
你能看出来有哪里不对吗
对,没错,倒数第三个字段cr,gsonformat自动生成的是int型的,而注意看json串里的sData集合里有三个对象,第一个对象的cr是77,第二个是23,第三个是12.3,那么问题就出在这里,gsonformat会根据第一个对象的cr是77来设定类型,所以自动生成为int型,而这个cr是有可能是带小数点的,所以应该为float或者double型才对,太坑了有没有!!!