Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android之ListView&Json加載網絡數據,androidlistview

Android之ListView&Json加載網絡數據,androidlistview

編輯:關於android開發

Android之ListView&Json加載網絡數據,androidlistview


使用到的主要內容:

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 } HttpUtil

 

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":"一米八左右 干淨利落不墨跡 有什麼好玩的 求大神帶我飛 隨時都可以約 帶我玩點新鮮的吧 報名了就來私啊

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved