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

相关文章

python获取当前文件和目录路径的方法详解

《python获取当前文件和目录路径的方法详解》:本文主要介绍Python中获取当前文件路径和目录的方法,包括使用__file__关键字、os.path.abspath、os.path.realp... 目录1、获取当前文件路径2、获取当前文件所在目录3、os.path.abspath和os.path.re

Java子线程无法获取Attributes的解决方法(最新推荐)

《Java子线程无法获取Attributes的解决方法(最新推荐)》在Java多线程编程中,子线程无法直接获取主线程设置的Attributes是一个常见问题,本文探讨了这一问题的原因,并提供了两种解决... 目录一、问题原因二、解决方案1. 直接传递数据2. 使用ThreadLocal(适用于线程独立数据)

使用Python检查CPU型号并弹出警告信息

《使用Python检查CPU型号并弹出警告信息》本教程将指导你如何编写一个Python程序,该程序能够在启动时检查计算机的CPU型号,如果检测到CPU型号包含“I3”,则会弹出一个警告窗口,感兴趣的小... 目录教程目标方法一所需库步骤一:安装所需库步骤二:编写python程序步骤三:运行程序注意事项方法二

PostgreSQL如何查询表结构和索引信息

《PostgreSQL如何查询表结构和索引信息》文章介绍了在PostgreSQL中查询表结构和索引信息的几种方法,包括使用`d`元命令、系统数据字典查询以及使用可视化工具DBeaver... 目录前言使用\d元命令查看表字段信息和索引信息通过系统数据字典查询表结构通过系统数据字典查询索引信息查询所有的表名可

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

业务中14个需要进行A/B测试的时刻[信息图]

在本指南中,我们将全面了解有关 A/B测试 的所有内容。 我们将介绍不同类型的A/B测试,如何有效地规划和启动测试,如何评估测试是否成功,您应该关注哪些指标,多年来我们发现的常见错误等等。 什么是A/B测试? A/B测试(有时称为“分割测试”)是一种实验类型,其中您创建两种或多种内容变体——如登录页面、电子邮件或广告——并将它们显示给不同的受众群体,以查看哪一种效果最好。 本质上,A/B测

【北交大信息所AI-Max2】使用方法

BJTU信息所集群AI_MAX2使用方法 使用的前提是预约到相应的算力卡,拥有登录权限的账号密码,一般为导师组共用一个。 有浏览器、ssh工具就可以。 1.新建集群Terminal 浏览器登陆10.126.62.75 (如果是1集群把75改成66) 交互式开发 执行器选Terminal 密码随便设一个(需记住) 工作空间:私有数据、全部文件 加速器选GeForce_RTX_2080_Ti

Java 后端接口入参 - 联合前端VUE 使用AES完成入参出参加密解密

加密效果: 解密后的数据就是正常数据: 后端:使用的是spring-cloud框架,在gateway模块进行操作 <dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>30.0-jre</version></dependency> 编写一个AES加密

Android Environment 获取的路径问题

1. 以获取 /System 路径为例 /*** Return root of the "system" partition holding the core Android OS.* Always present and mounted read-only.*/public static @NonNull File getRootDirectory() {return DIR_ANDR

每日一练7:简写单词(含链接)

1.链接 简写单词_牛客题霸_牛客网 2.题目 3.代码1(错误经验) #include <iostream>#include <string>using namespace std;int main() {string s;string ret;int count = 0;while(cin >> s)for(auto a : s){if(count == 0){if( a <=