欢迎来到天天文库
浏览记录
ID:14120489
大小:218.00 KB
页数:8页
时间:2018-07-26
《listview加载网络数据和图片》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、ListView加载网络数据和图片如,从服务器端获得商品名称、价格、简介和图片,加载到AndroidListView中。又如加载微博内容。需了解熟悉:1、ListView行布局,排版,getView方法这个案例可以说是实现ListView图文混排的网络版。关于在ListView中实现排版的方法,参考:2、异步任务的实现,Handler+Thread,AsyncTask3、JSON解析方式实现思路:1、异步加载服务器访问商品数据(json格式)封装网络访问的方法2、json数据转为Adapter数据(List>)封装数据转换的方法3、
2、Adapter先加载文本内容信息使用AsyncTask加载网络访问的集合数据加载ListView中的文本信息4、json数据中有图片信息(路径),异步读取加载图片采用接口回调的方法,加载图片信息(Handler+Thread)publicclassHttpUtil{publicstaticfinalStringBASE_URL="http://10.0.2.2:8080/jsontest/servlet/ProductServlet";publicstaticfinalStringIMG_URL="http://10.0.2.2:8080/jsontest/upload
3、/";publicstaticHttpClienthttpClient=newDefaultHttpClient();//post方法访问服务器,返回json字符串publicstaticStringgetRequest(Stringurl){Stringresult=null;HttpGethttpGet=newHttpGet(url);try{HttpResponsehttpResponse=httpClient.execute(httpGet);if(httpResponse.getStatusLine().getStatusCode()==200){result
4、=EntityUtils.toString(httpResponse.getEntity(),"utf-8");}}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returnresult;}//字符串转成集合数据publicstaticvoidresultString2List(List>list,Stringstr,Stringtitle){try{JSONObjectjsonObject=newJSONObject(str);JSONAr
5、rayjsonArray=jsonObject.getJSONArray(title);for(inti=0;imap=newHashMap();Iteratoriterator=jsonObject2.keys();while(iterator.hasNext()){Stringkey=iterator.next();Objectvalue=
6、jsonObject2.get(key);map.put(key,value);}list.add(map);}}catch(JSONExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}//post方法访问服务器,返回集合数据publicstaticList>getRequest2List(Stringurl,Stringtitle){List>list=newArrayList>(
7、);resultString2List(list,url,title);returnlist;}//get方法访问服务器,返回json字符串publicstaticStringpostRequest(Stringurl,MaprawParams)throwsException{HttpPostpost=newHttpPost(url);Listparams=newArrayList();for(Stringkey:rawParams.keySet()
此文档下载收益归作者所有