Android Retrofit2 Data Parsing Code Parsing

  • 2021-12-09 10:13:29
  • OfStack

I wasted a long time in data analysis. At first, I felt that the type that was transmitted and received by objects was json, so I thought about it in that way. It took a long time.

After reading what others wrote, I realized that it was really simple. Thanks to https://www.jianshu.com/p/d0081e8a7edc for inspiring me.

Added jar package

//Retrofit Library
implementation 'com.squareup.retrofit2:retrofit:2.0.2'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'

Data from the server:

{
"code":0,
"resultMsg":"OJBK",
"resultState":"SUCCESS",
"resultObj":[
{"id": null, "nickname": "Koko", "head_img": null,},
{"id": null, "nickname": "Koko", "head_img": null,
{"id": null, "nickname": "Koko", "head_img": null,
{"id": null, "nickname": "Koko", "head_img": null,},
{"id": null, "nickname": "likui jy", "head_img": ",}]}

Create an WebResponse object to receive data:


package controller.hzl.com.hzl1.model;

import java.util.List;

import controller.hzl.com.hzl1.entity.HNOTICELOG_E;

public class WebRespone {

  /**
   *  Return status code 
   */
  private Integer code;

  /**
   *  Return message 
   */
  private Object resultMsg;

  /**
   *  Return results 
   */
  private String resultState;


  /**
   *  Returns a data object 
   */
  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;
  }
}

Note that one of this object is of list type.


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;
  }
}

Equivalent to an List directly below resultObj. In this way, you can directly receive the List object in json transmitted by the server.


Related articles: