數(shù)據(jù)準(zhǔn)備
本案例中的服務(wù)端數(shù)據(jù)以Json的形式傳遞,在服務(wù)端使用.Net開發(fā)一個一般處理程序,序列化一個產(chǎn)品對象,里面包含名稱、價格、圖片名稱,最后序列化成JSON格式的數(shù)據(jù)返回給客戶端。
數(shù)據(jù)如下
1 [{"imageName":"image1.png","name":"蘋果","price":12},2 {"imageName":"image2.png","name":"鬧鐘","price":56},3 {"imageName":"image3.png","name":"蛋糕","price":24},4 {"imageName":"image4.png","name":"零錢包","price":8},5 {"imageName":"image5.png","name":"書本","price":42},6 {"imageName":"image6.png","name":"糖果","price":16},7 {"imageName":"image7.png","name":"西瓜","price":2}]本案例的URL地址均使用一個CommonUri類進(jìn)行管理:
1 package com.example.handlerimageortext;2 3 public class CommonUri {4     // 訪問服務(wù)器數(shù)據(jù)的鏈接5     public static final String PRODUCT_URL = "http://192.168.1.102:1231/json/returnCommondityJson.ashx";6     // 圖片的連接7     public static final String PRODUCT_IMG="http://192.168.1.102:1231/json/img/";8 }
使用AsyncTask獲取Json數(shù)據(jù)
在UI線程中,使用AsyncTask的方式訪問網(wǎng)絡(luò)獲取JSON數(shù)據(jù),并對其進(jìn)行解析
 1     public class MyTask extends AsyncTask<String, Void, List<Map<String,Object>>>{ 2         @Override 3         protected void onPreExecute() { 4             super.onPreExecute(); 5             // 顯示對話框 6             dialog.show(); 7         } 8          9         @Override10         protected List<Map<String, Object>> doInBackground(String... params) {11             List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();12             try {13                 // 獲取網(wǎng)絡(luò)JSON格式數(shù)據(jù)14                 HttpClient httpClient=new DefaultHttpClient();15                 HttpPost httpPost=new HttpPost(params[0]);16                 HttpResponse httpResponse=httpClient.execute(httpPost);17                 if(httpResponse.getStatusLine().getStatusCode()==200){18                     String jsonString=EntityUtils.toString(httpResponse.getEntity(),"utf-8");19                     // 解析Json格式數(shù)據(jù),并使用一個List<Map>存放20                     JSONArray jsonArray=new JSONArray(jsonString);21                     for(int i=0;i<jsonArray.length();i++){22                         JSONObject jsonObject=jsonArray.getJSONObject(i);23                         Map<String,Object> map=new HashMap<String, Object>();24                         map.put("name",jsonObject.get("name"));25                         map.put("price",jsonObject.get("price"));26                         map.put("imageName",jsonObject.get("imageName"));27                         list.add(map);28                     }29                 }30             } catch (Exception e) {31                 e.printStackTrace();32             }33             return list;34         }35         @Override36         protected void onPostExecute(List<Map<String, Object>> result) {37             super.onPostExecute(result);38             // 把查詢到的數(shù)據(jù)傳遞給適配器39             adapter.setData(result);40             // 為ListView設(shè)定適配器41             listview.setAdapter(adapter);42             adapter.notifyDataSetChanged();43             // 隱藏對話框44             dialog.dismiss();45         }        46     }
新聞熱點(diǎn)
疑難解答
圖片精選