本文主要是介绍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类 Newspublic 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×tamp=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×tamp=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×tamp=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×tamp=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×tamp=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×tamp=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×tamp=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×tamp=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×tamp=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×tamp=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×tamp=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获取网路链接展示接口信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!