NewsInfo实体类
package com.example.week14_02news;
public class NewsInfo {
private String icon;
private String title;
private String content;
private int type;
private long comment;
public NewsInfo(String icon, String title, String content, int type, long comment) {
this.icon = icon;
this.title = title;
this.content = content;
this.type = type;
this.comment = comment;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public long getComment() {
return comment;
}
public void setComment(long comment) {
this.comment = comment;
}
}
解析json工具
package com.example.week14_02news;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;
public class JsonParse {
public static List<NewsInfo> getNewsInfo(String json) {
Gson gson = new Gson();
Type listType = new TypeToken<List<NewsInfo>>() {
}.getType();
List<NewsInfo> newsInfos = gson.fromJson(json, listType);
return newsInfos;
}
}
MainActivity
package com.example.week14_02news;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle