【Android】新闻列表http与listview练习

这篇博客详细介绍了如何在Android平台上实现新闻列表的显示,包括使用http请求获取数据,通过NewsInfo实体类解析JSON,运用MainActivity进行业务逻辑处理,以及设计activity_main和news_item布局文件来展示新闻条目。
摘要由CSDN通过智能技术生成

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;

/**
 * 需要导入jar包 gson
 * 解析 json---> List<T>集合类型
 */
public class JsonParse {
   
    public static List<NewsInfo> getNewsInfo(String json) {
   
        //解析JSON数据
        Gson gson = new Gson();
        //TypeToken对象的getType()方法,要转换成什么类型
        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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_popo_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值