Android Retrofit2数据解析代码解析
在弄数据解析这块,浪费了很长的时间,最开始一直觉得传过来用对象接收的,类型是json,往那个方式去想了。搞了很久。
后来看了别人写的才发觉,真是很简单,感谢https://www.jianshu.com/p/d0081e8a7edc给我启发。
加入的jar包
//Retrofit库
implementation'com.squareup.retrofit2:retrofit:2.0.2'
implementation'com.squareup.retrofit2:converter-gson:2.0.2'
服务端传过来的数据:
{
"code":0,
"resultMsg":"OJBK",
"resultState":"SUCCESS",
"resultObj":[
{"id":null,"nickname":"科科","head_img":null,},
{"id":null,"nickname":"科科","head_img":null,},
{"id":null,"nickname":"科科","head_img":null,},
{"id":null,"nickname":"科科","head_img":null,},
{"id":null,"nickname":"李逵","head_img":"",}]}
创建一个WebResponse对象,来接收数据:
packagecontroller.hzl.com.hzl1.model; importjava.util.List; importcontroller.hzl.com.hzl1.entity.HNOTICELOG_E; publicclassWebRespone{ /** *返回状态码 */ privateIntegercode; /** *返回消息 */ privateObjectresultMsg; /** *返回结果 */ privateStringresultState; /** *返回数据对象 */ privateListresultObj; publicIntegergetCode(){ returncode; } publicObjectgetResultMsg(){ returnresultMsg; } publicStringgetResultState(){ returnresultState; } publicvoidsetCode(Integercode){ this.code=code; } publicvoidsetResultMsg(ObjectresultMsg){ this.resultMsg=resultMsg; } publicvoidsetResultState(StringresultState){ this.resultState=resultState; } publicList getResultObj(){ returnresultObj; } publicvoidsetResultObj(List resultObj){ this.resultObj=resultObj; } }
注意,这个对象里面有一个是list类型。
publicclassHNOTICELOG_E{ privateLongid; privateStringnickname; privateStringhead_img; privateStringnoticeContext; privateStringrelUserId; privateStringrelNoticeUserId; privateDatecreateTime; privateStringcreateUser; privateDateupdateTime; privateStringupdateUser; privateIntegerstatus; publicLonggetId(){ returnid; } publicvoidsetId(Longid){ this.id=id; } publicStringgetNoticeContext(){ returnnoticeContext; } publicvoidsetNoticeContext(StringnoticeContext){ this.noticeContext=noticeContext==null?null:noticeContext.trim(); } publicStringgetRelUserId(){ returnrelUserId; } publicvoidsetRelUserId(StringrelUserId){ this.relUserId=relUserId==null?null:relUserId.trim(); } publicStringgetRelNoticeUserId(){ returnrelNoticeUserId; } publicvoidsetRelNoticeUserId(StringrelNoticeUserId){ this.relNoticeUserId=relNoticeUserId==null?null:relNoticeUserId.trim(); } publicDategetCreateTime(){ returncreateTime; } publicvoidsetCreateTime(DatecreateTime){ this.createTime=createTime; } publicStringgetCreateUser(){ returncreateUser; } publicvoidsetCreateUser(StringcreateUser){ this.createUser=createUser==null?null:createUser.trim(); } publicDategetUpdateTime(){ returnupdateTime; } publicvoidsetUpdateTime(DateupdateTime){ this.updateTime=updateTime; } publicStringgetUpdateUser(){ returnupdateUser; } publicvoidsetUpdateUser(StringupdateUser){ this.updateUser=updateUser==null?null:updateUser.trim(); } publicIntegergetStatus(){ returnstatus; } publicvoidsetStatus(Integerstatus){ this.status=status; } publicStringgetNickname(){ returnnickname; } publicvoidsetNickname(Stringnickname){ this.nickname=nickname; } publicStringgetHead_img(){ returnhead_img; } publicvoidsetHead_img(Stringhead_img){ this.head_img=head_img; } }
相当于是resultObj下面的直接是一个List。这样,就直接能够接收到服务端传的json里面的List对象。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。