Android之ListView&Json加载网络数据
Posted 西门吃雪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android之ListView&Json加载网络数据相关的知识,希望对你有一定的参考价值。
使用到的主要内容:
1、Json 解析网络数据
2、异步任务加载图片和数据
3、ListView 的内存空间优化(ConvertView)和运行时间优化(ViewHolder)
4、ListView 滚动监听实现分页加载数据
5、动态定义 布局和控件(想实现下拉刷新,好像不是这样实现的..)
.......
HttpUtil 工具类网络申请数据(用 Gson 解析)
--- 别忘记添加网络权限
<uses-permission android:name="android.permission.INTERNET"/>
1 package com.dragon.android.a09_09; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 9 /** 10 * Created by Auser on 2016/9/7. 11 */ 12 public class HttpUtil { 13 14 public static byte[] loadByte(String url){ 15 16 HttpURLConnection conn = null; 17 InputStream is = null; 18 ByteArrayOutputStream baos = null; 19 try { 20 conn = (HttpURLConnection) new URL(url).openConnection(); 21 if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { 22 is = conn.getInputStream(); 23 baos = new ByteArrayOutputStream(); 24 25 byte[] buffer = new byte[1024]; 26 int len = -1; 27 while ((len = is.read(buffer)) != -1) { 28 baos.write(buffer,0,len); 29 } 30 return baos.toByteArray(); 31 } 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } finally { 35 try { 36 if (baos!=null){ 37 baos.flush(); 38 baos.close(); 39 } 40 if (is != null) { 41 is.close(); 42 } 43 if (conn != null) { 44 conn.disconnect(); 45 } 46 } catch (IOException e) { 47 e.printStackTrace(); 48 } 49 } 50 return null; 51 } 52 53 }
Datas 类存放需要的数据(GsonFormat 插件)
1 package com.dragon.android.a09_09; 2 3 import java.util.List; 4 5 /** 6 * Created by Auser on 2016/9/9. 7 */ 8 public class Datas { 9 10 /** 11 * code : 99999 12 * data : {"results":[{"action":0,"actionTime":1472219620657,"candidateCount":85,"caterAvgPrice":"49","caterBranchName":"中心城店","caterBusinessId":"DZ:2213221","caterCategories":"电影院","caterCity":"深圳","caterPhotoUrl":"http://qcloud.dpfile.com/pc/EXgo-1x_xBtU9ZucqX7hTSvd27a8ZWYntDVUaMciJtZbnpM9T49yMmnt4yPiPxuwTYGVDmosZWTLal1WbWRW3A.jpg","caterPlatform":1,"caterRegions":"福田区 市中心区","caterSPhotoUrl":"http://qcloud.dpfile.com/pc/EXgo-1x_xBtU9ZucqX7hTSvd27a8ZWYntDVUaMciJtZwpuORpFhuvgx7Yzr27-sHTYGVDmosZWTLal1WbWRW3A.jpg","caterShopId":382401,"caterTelephone":"0755-82801168","clientStamp":1472219156,"commentCount":201,"createTime":1472219620657,"credit":0,"deviceModel":"iPhone","eventAddress":"福田区福华一路怡景中心城G层","eventCity":"0755","eventCityName":"深圳","eventDateTime":1473420600000,"eventDescription":"简介:\\n对于不会说人话情商低,公主病的,\\n满嘴脏话,不懂得做人做事的,\\n没时间更没兴趣撩你,套路你。\\n吃瓜群众别瞎BB,东北人没那么好的脾气,\\n会开玩笑的开,开的起玩笑的开,别装逼,\\n长得好看你是优势,毕竟是个看脸的时代,\\n但你没素质没礼貌就别老自以为自己很美。\\n人丑不可怕,就怕你心也丑,做好你自己。\\n每个人都差钱,谁也不是银行,但你别差事。\\n做人都是相互的,你帮帮我我帮帮你,\\n但别觉得一切都是理所当然的。\\n能做兄弟做兄弟,能做朋友做朋友,爽快人\\n别磨叽。\\n\\n确定艺人:汪涵,林志颖、任贤齐、\\n 萧亚轩、潘玮柏、李荣浩。\\n\\n待定艺人:许巍","eventExpense":47,"eventKey":"LFCAEL","eventLatitude":22.53608,"eventLocation":"金逸影城","eventLocationUrl":"http://m.dianping.com/shop/2213221?utm_source=open","eventLongitude":114.05923,"eventName":"有趣的灵魂一起去大运体育中心看演唱会","eventRegion":"福田区 市中心区","fee":1,"feeType":{"desc":"我请客","field":"MyTreat","value":1},"guarantee":0,"id":827518,"isMark":false,"multi":0,"opposite":0,"rechargeCred":0,"score":653.0702748285551,"showCount":3670,"state":0,"tagId":"3706","url":"http://www.qingchifan.com/event/detail/827518","user":{"action":1,"age":77,"constellation":"双鱼座","gender":1,"height":180,"isOfficial":"0","lastOnlineTime":"1473358886081","lat":22.54983991655006,"lng":114.1008416972523,"nick":"Mr.Yang","role2":"0","userId":127891,"userImageUrl":"http://img2.chouti.com/QIM/2016090901/7dff30c8e35047fda84bec83128af793.jpg","userKey":"EFCLME"},"userId":127891,"visitorState":0},{"action":0,"actionTime":1473378002766,"candidateCount":1,"caterAvgPrice":"288","caterBranchName":"","caterBusinessId":"DZ:2810071","caterCategories":"自助餐","caterCity":"深圳","caterPhotoUrl":"http://qcloud.dpfile.com/pc/ka5nJiUVP1iEDEInm-5ctFhAg0oTJ6h_JrWwPWyTVSvV-Oie0zXWa0hU-eqGUQgMTYGVDmosZWTLal1WbWRW3A.jpg","caterPlatform":1,"caterRegions":"福田区 市中心区","caterSPhotoUrl":"http://qcloud.dpfile.com/pc/ka5nJiUVP1iEDEInm-5ctFhAg0oTJ6h_JrWwPWyTVSsJDGuWFTd4kmkKP90dcyjBTYGVDmosZWTLal1WbWRW3A.jpg","caterShopId":74869,"caterTelephone":"0755-21513838","clientStamp":1473378002086,"commentCount":5,"createTime":1473378002766,"credit":130,"deviceModel":"Android","eventAddress":"福田区益田路4088号香格里拉大酒店1楼","eventCity":"0755","eventCityName":"深圳","eventDateTime":1474362000000,"eventDescription":"人品是当今社会稀缺而珍贵的品质标签,人品决定态度,态度决定行为,行为决定着最后的结果。女人外表漂亮固然惹人喜爱,但智慧以及谦卑的态度也不可缺少,否则,人就很容易显出骄傲自大、爱慕虚荣等丑恶的特质。","eventExpense":288,"eventKey":"LGLLED","eventLatitude":22.536156,"eventLocation":"福田香格里拉大酒店-鲜咖啡自助餐厅","eventLocationUrl":"http://m.dianping.com/shop/2810071?utm_source=open","eventLongitude":114.057,"eventName":"Buffet\\n自助餐","eventRegion":"市中心区","fee":1,"feeType":{"desc":"我请客","field":"MyTreat","value":1},"guarantee":0,"guaranteeCred":0,"id":838810,"isMark":false,"multi":0,"opposite":2,"rechargeCred":0,"score":510.20296508423274,"showCount":22,"state":0,"tagId":"3792","url":"http://www.qingchifan.com/event/detail/838810","user":{"action":1,"age":28,"constellation":"白羊座","gender":1,"height":170,"isOfficial":"0","lastOnlineTime":"1473382864509","lat":22.650712,"lng":114.024987,"nick":"梦卧客","role2":"0","userId":35965,"userImageUrl":"http://img2.chouti.com/group/2016082810/1942adb39d8b69a37fa0887fd55bab9b.jpg","userKey":"GAMBA"},"userId":35965,"visitorState":0},{"action":0,"actionTime":1472372122903,"candidateCount":76,"caterAvgPrice":"366","caterBranchName":"深圳华侨城店","caterBusinessId":"DZ:2723608","caterCategories":"牛排 西餐","caterCity":"深圳","caterPhotoUrl":"http://i3.s2.dpfile.com/pc/f14584fdd425d9a5a70f747f7ebcc6fa(700x700)/thumb.jpg","caterPlatform":1,"caterRegions":"南山区 华侨城 白石洲 益田假日","caterSPhotoUrl":"http://i2.s2.dpfile.com/pc/f14584fdd425d9a5a70f747f7ebcc6fa(278x200)/thumb.jpg","caterShopId":1120,"caterTelephone":"0755-86298729","clientStamp":1472372124606,"commentCount":55,"createTime":1472372122903,"credit":0,"deviceModel":"Android","eventAddress":"南山区深南大道9028号益田假日广场3F","eventCity":"0755","eventCityName":"深圳","eventDateTime":1473417000000,"eventDescription":"一米八左右 干净利落不墨迹 有什么好玩的 求大神带我飞 随时都可以约 带我玩点新鲜的吧 报名了就来私啊以上是关于Android之ListView&Json加载网络数据的主要内容,如果未能解决你的问题,请参考以下文章
OnClick 打开 URL (Json, ListView) Android
如何使用json解析android在listview中获取图像