时间:2021-07-11 08:40:51 | 栏目:Android代码 | 点击:次
在弄数据解析这块,浪费了很长的时间,最开始一直觉得传过来用对象接收的,类型是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对象,来接收数据:
package controller.hzl.com.hzl1.model; import java.util.List; import controller.hzl.com.hzl1.entity.HNOTICELOG_E; public class WebRespone { /** * 返回状态码 */ private Integer code; /** * 返回消息 */ private Object resultMsg; /** * 返回结果 */ private String resultState; /** * 返回数据对象 */ private List<HNOTICELOG_E> resultObj; public Integer getCode() { return code; } public Object getResultMsg() { return resultMsg; } public String getResultState() { return resultState; } public void setCode(Integer code) { this.code = code; } public void setResultMsg(Object resultMsg) { this.resultMsg = resultMsg; } public void setResultState(String resultState) { this.resultState = resultState; } public List<HNOTICELOG_E> getResultObj() { return resultObj; } public void setResultObj(List<HNOTICELOG_E> resultObj) { this.resultObj = resultObj; } }
注意,这个对象里面有一个是list类型。
public class HNOTICELOG_E { private Long id; private String nickname; private String head_img; private String noticeContext; private String relUserId; private String relNoticeUserId; private Date createTime; private String createUser; private Date updateTime; private String updateUser; private Integer status; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getNoticeContext() { return noticeContext; } public void setNoticeContext(String noticeContext) { this.noticeContext = noticeContext == null ? null : noticeContext.trim(); } public String getRelUserId() { return relUserId; } public void setRelUserId(String relUserId) { this.relUserId = relUserId == null ? null : relUserId.trim(); } public String getRelNoticeUserId() { return relNoticeUserId; } public void setRelNoticeUserId(String relNoticeUserId) { this.relNoticeUserId = relNoticeUserId == null ? null : relNoticeUserId.trim(); } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getCreateUser() { return createUser; } public void setCreateUser(String createUser) { this.createUser = createUser == null ? null : createUser.trim(); } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } public String getUpdateUser() { return updateUser; } public void setUpdateUser(String updateUser) { this.updateUser = updateUser == null ? null : updateUser.trim(); } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getHead_img() { return head_img; } public void setHead_img(String head_img) { this.head_img = head_img; } }
相当于是resultObj 下面的直接是一个List。这样,就直接能够接收到服务端传的json里面的List对象。