ListView获取网路链接展示接口信息

2024-03-06 03:20

本文主要是介绍ListView获取网路链接展示接口信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

例:




HttpURLConnection网络请求,
ImageLoader获取图片

在Module的build.gradle文件中添加依赖和属性配置(记得还有第三方共享的jar包要导):

dependencies {
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.google.code.gson:gson:2.2.4'
}


MainActivity类:
public class MainActivity extends AppCompatActivity {private ListView lv;private Handler myHandler=new Handler(){public void handleMessage(android.os.Message msg) {List<News.NewslistBean> lists= (List<News.NewslistBean>) msg.obj;MyAdapter myAdapter = new MyAdapter(MainActivity.this, lists);lv.setAdapter(myAdapter);};};@Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);lv = (ListView) findViewById(R.id.lv);new Thread(){public void run(){try {URL url=new URL("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1");HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");connection.setReadTimeout(5000);connection.setConnectTimeout(5000);int code=connection.getResponseCode();if(code==200){//5
                        InputStream inputStream = connection.getInputStream();String content=streamToString(inputStream);Gson gson=new Gson();News news = gson.fromJson(content, News.class);List<News.NewslistBean> newslist = news.getNewslist();Message msg=Message.obtain();msg.obj=newslist;myHandler.sendMessage(msg);}} catch (Exception e) {e.printStackTrace();}}}.start();}public String streamToString(InputStream is){StringBuilder builder=new StringBuilder();BufferedReader reader=new BufferedReader(new InputStreamReader(is));String con;try {while((con=reader.readLine())!=null){builder.append(con);}reader.close();} catch (Exception e) {e.printStackTrace();}return builder.toString();}
}
 

微笑微笑微笑activity_main

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.samsung.sfl10191.MainActivity">
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lv"
    ></ListView>
</android.support.constraint.ConstraintLayout>




微笑微笑微笑适配器MyAdapter类

public class MyAdapter extends BaseAdapter{Context con;List<News.NewslistBean> lists;public MyAdapter(Context con, List<News.NewslistBean> lists) {this.con=con;this.lists=lists;}@Override
    public int getCount() {return lists.size();}@Override
    public Object getItem(int position) {return lists.get(position);}@Override
    public long getItemId(int position) {return position;}@Override
    public View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder;if(convertView==null){holder = new ViewHolder();convertView = View.inflate(con,R.layout.item,null);holder.img = (ImageView) convertView.findViewById(R.id.img);holder.title = (TextView) convertView.findViewById(R.id.tv_title);convertView.setTag(holder);}else{holder = (ViewHolder) convertView.getTag();}holder.title.setText(lists.get(position).getTitle());ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(con));ImageLoader.getInstance().displayImage(lists.get(position).getPicUrl().toString(),holder.img);return convertView;}class ViewHolder {TextView title;ImageView img;}
}
item 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"><ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/img"/><TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_title"
        /></LinearLayout>


 

Bean类可以换成这个链接:http://www.xieast.com/api/news/news.php

(记得上面的链接也要一起换)

Bean类 News
public class News {/**
     * code : 200
     * msg : success
     * newslist : [{"ctime":"2017-10-19","title":"财新将全面收费 \u201c财新通\u201d问世","description":"健康点healthpoint","picUrl":"https://t1.qpic.cn/mblogpic/34d9dfb75cfceb04a840/2000","url":"https://mp.weixin.qq.com/s?src=16&ver=435&timestamp=1508371237&signature=UtWCCzY7cc41Os1d*zC5vmHTSC16K*ObDmVLSMPEGBGevaU7Iv7eS6lWazuYAmrmioKb2AjS70n6pAWLS1yVyP8GenoWkX3qSFotv90gnQY="},{"ctime":"2017-10-19","title":"A股重要时间窗,谁是最大赢家?近13亿元大单抢筹12只个股(名单)","description":"和讯股票","picUrl":"https://t1.qpic.cn/mblogpic/f01a972dbcc1060fd456/2000","url":"https://mp.weixin.qq.com/s?src=16&ver=435&timestamp=1508371236&signature=hS6p51Z3mxkZjPaoTF3reR8jhlW2icEP0ONmPGoAdBkFOcVHXTcweULe7X3LpeNBFji59GoaP0uRG04wsfO2JDMvoUqViS0m8Ox-ghygPJQ="},{"ctime":"2017-10-19","title":"【晨报健康】家长们注意!宝宝爱咬指甲是缺微量元素,还可能是心理问题","description":"许昌晨报","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-54523025.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=435&timestamp=1508371236&signature=V8REkzFTdHAoMo3X6rM49RadrAtk7uS-HPQzIy36kyO*o3smsFAMdueqRSzZMTbYOEXnxwVn7be5cKmY-IJ2zAZM8904iOzwbddRFaUtYOg="},{"ctime":"2017-10-19","title":"青山之恋 青春之恋 看《青恋》","description":"FM95浙江经济广播","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-34497627.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=435&timestamp=1508371236&signature=Dwg9MLnjMi*08GMHfMCv8hwga2zSWN5apJtgOY3grPqpPJ2MRT8OLPWNXt2Cb-HYjPySj5eZ*FX9IT*qQjWAx97cz-AB5DOcATs0hZCXr08="},{"ctime":"2017-10-19","title":"看完近40所热门民办幼升小3年面试真题,我发现英语应该这样准备","description":"金融辣妈","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-55332312.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=435&timestamp=1508371235&signature=SnczEF4q6JlHR9btdgUs8ZDTRbuccMx5veRDpiNeSYog9W6QVaZttSipOSi*KnHmRrACgRpj9Y-wqMMFMyNoIh3QO8F5-xgJyD0cCGuZCb0="},{"ctime":"2017-10-19","title":"【晨报生活】家里装了这种水龙头的都看看!必须马上清理","description":"许昌晨报","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-54453832.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=435&timestamp=1508371234&signature=vfXQVBJRwtCm72RJ0dEBeOxqJJl3vT1bqyLpFymEzjFxo0Pjh3-fTiYcYhhK9mG9p2sC2YQU8-nymSFtucyl64xxmlnjMzON0-o6kI5vxO0="},{"ctime":"2017-10-19","title":"小小常学成语| 囫囵吞枣","description":"常青藤爸爸","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-51774641.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=435&timestamp=1508371234&signature=hcOVVSRjeJdwbNTtxQ55l2T14sP7EEpBJPv8R6p9n2yZLwbhXLuI-Yv3*0*86P9pRYhJCl*b-HMGOFl3ef9cqqiJHl7qteBjdqnK9kak9co="},{"ctime":"2017-10-19","title":"中国真的是发展中国家?答案残忍,让无数西方人满身冷汗!","description":"墨者善狩","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-54993853.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=435&timestamp=1508371233&signature=LmfrXWxk3w2nIcCR16IgPp0g5ZikcS77PIKePd41n5OaiXUooVs4Bhk1kZ2nvR7bfst2MX8VgUj7jIDP12MEGq7z5wlS*tsPMjTVCS-HGDk="},{"ctime":"2017-10-19","title":"橡胶:企稳上涨还需时日","description":"期货日报","picUrl":"https://t1.qpic.cn/mblogpic/f01a972dbcc1060fd456/2000","url":"https://mp.weixin.qq.com/s?src=16&ver=435&timestamp=1508371233&signature=28obULKpBc*Z84dxynArFR*6yzY0JRqq9P6mAUdbYcxOZWHsyQHI*SwsztAzBZkWH8TffGCcPu6FfHHy8kiLk3jAmlurnbNKobV61PbuFdM="},{"ctime":"2017-10-19","title":"下游需求良好,市场购销活跃:国内新产大豆量增价升","description":"期货日报","picUrl":"https://t1.qpic.cn/mblogpic/f01a972dbcc1060fd456/2000","url":"https://mp.weixin.qq.com/s?src=16&ver=435&timestamp=1508371233&signature=M-y2V58Cp1wLX41cJtGT1vs3Ua4VKArCEbp9yBzO3*ZraloJOhYoCAgv3JiE7q-cMG2KJH41nqs2JTOR3LLbLhxOITSx6HRtEUznPEiW8l8="}]
     */

    private int code;private String msg;private List<NewslistBean> newslist;public int getCode() {return code;}public void setCode(int code) {this.code = code;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public List<NewslistBean> getNewslist() {return newslist;}public void setNewslist(List<NewslistBean> newslist) {this.newslist = newslist;}public static class NewslistBean {/**
         * ctime : 2017-10-19
         * title : 财新将全面收费 财新通问世
         * description : 健康点healthpoint
         * picUrl : https://t1.qpic.cn/mblogpic/34d9dfb75cfceb04a840/2000
         * url : https://mp.weixin.qq.com/s?src=16&ver=435&timestamp=1508371237&signature=UtWCCzY7cc41Os1d*zC5vmHTSC16K*ObDmVLSMPEGBGevaU7Iv7eS6lWazuYAmrmioKb2AjS70n6pAWLS1yVyP8GenoWkX3qSFotv90gnQY=
         */

        private String ctime;private String title;private String description;private String picUrl;private String url;public String getCtime() {return ctime;}public void setCtime(String ctime) {this.ctime = ctime;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public String getPicUrl() {return picUrl;}public void setPicUrl(String picUrl) {this.picUrl = picUrl;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}}
}

这篇关于ListView获取网路链接展示接口信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/778721

相关文章

MySQL 获取字符串长度及注意事项

《MySQL获取字符串长度及注意事项》本文通过实例代码给大家介绍MySQL获取字符串长度及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 获取字符串长度详解 核心长度函数对比⚠️ 六大关键注意事项1. 字符编码决定字节长度2

python3如何找到字典的下标index、获取list中指定元素的位置索引

《python3如何找到字典的下标index、获取list中指定元素的位置索引》:本文主要介绍python3如何找到字典的下标index、获取list中指定元素的位置索引问题,具有很好的参考价值,... 目录enumerate()找到字典的下标 index获取list中指定元素的位置索引总结enumerat

SpringBoot+Redis防止接口重复提交问题

《SpringBoot+Redis防止接口重复提交问题》:本文主要介绍SpringBoot+Redis防止接口重复提交问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录前言实现思路代码示例测试总结前言在项目的使用使用过程中,经常会出现某些操作在短时间内频繁提交。例

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP