使用JSON获得服务器的数据并存放在List中

从服务器获得数据如下所示:

[{"pk": 2, "model": "innovate.speaker", "fields": {"picture":
"/static/i/speakers/01_alavilli_r5.png", "sessions": [40], "title":
"Developer Evangelist, PayPal", "name": "Praveen Alavilli", "details":
"Praveen Alavilli is the developer evangelist for the PayPal X Developer
Network, helping developers to monetize their ideas and applications
using the PayPal Payments Platform. Prior to PayPal, Praveen worked at
Amazon.com and AOL. "}},{...},{...}]

public List<Speaker> retrieveAllSpeakers(Activity activity){
        //将资源标识符转为字符串
        String url = activity.getResources().getString(R.string.feeds_speakers);
        //存储获得得Speaker,用于将数据返回
        List<Speaker> speakerArrayList = new ArrayList<Speaker>();
        //指定服务器端URL
        HttpGet httpGet = new HttpGet(url);
        
        HttpClient httpClient = new DefaultHttpClient();
        
        try {
            HttpResponse httpResponse =  httpClient.execute(httpGet);
            //获取响应实体
            HttpEntity httpEntity = httpResponse.getEntity();
            //将响应实体转换为字符串
            String jsonString = EntityUtils.toString(httpEntity);
            
            //将字符串转为JSONArray
            JSONArray jsonArray = new JSONArray(jsonString);
            Speaker speaker;
                
            for(int i = 0;i < jsonArray.length();i ++){
                /**
                 * 服务器返回的数据是JSONArray,在JSONArray里面有不同的JSONObject,
                 * 在JSONObject中“fields“名称后面得值是JSONObject,
                 * 我们需要的就是这个JSONObject
                 */
                JSONObject jsonObj = jsonArray.getJSONObject(i).getJSONObject("fields");
                //初始化Speaker
                speaker = new Speaker();
                    
                //获得jsonObj中的"name"名称后面得值,并保存在speaker中的name域中
                speaker.setName(jsonObj.getString("name"));
                //同上面得理由
                speaker.setDetails(jsonObj.getString("details"));
                //同上面的理由
                speaker.setPicture(jsonObj.getString("picture"));
                /**
                 * 由于sessions后面的值是一个JSONArray数组
                 * 所以得先获得这个数组,然后将数组里面得值存放
                 * 在List中
                 */
                JSONArray jsonArrayTmp = jsonObj.getJSONArray("sessions");
                List<String> tmp = new ArrayList<String>();
                for(int j = 0;j < jsonArrayTmp.length();j ++)
                {
                    tmp.add(jsonArrayTmp.getString(j));
                        
                }
                speaker.setSessionIds(tmp);
                //获得jsonObj中的"title"名称后面得值,并保存在speaker中的title域中
                speaker.setTitle(jsonObj.getString("title"));
                    
                //将speaker放到speakerArrayList中去
                speakerArrayList.add(speaker);
                    
            }
         
        }catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
        
        return speakerArrayList;
    }

Speaker类如下:

 1 import java.io.Serializable;
 2 import java.util.List;
 3 
 4 public class Speaker implements Serializable {
 5     private String name;
 6     
 7     private String title;
 8 
 9     private String picture;
10     
11     private String details;
12     
13     private List<String> sessionIds;
14     
15     public String getName() {
16         return name;
17     }
18     
19     public void setName(String name) {
20         this.name = name;
21     }
22     
23     public String getTitle() {
24         return title;
25     }
26     
27     public void setTitle(String title) {
28         this.title = title;
29     }
30     
31     public String getPicture() {
32         return picture;
33     }
34     
35     public void setPicture(String picture) {
36         this.picture = picture;
37     }
38     
39     public String getDetails() {
40         return this.details;
41     }
42     
43     public void setDetails(String details) {
44         this.details = details;
45     }
46     
47     public List<String> getSessionIds() {
48         return this.sessionIds;
49     }
50     
51     public void setSessionIds(List<String> sessionIds) {
52         this.sessionIds = sessionIds;
53     }
54 
55 }

 参考资料:http://www.cnblogs.com/jyan/articles/2544974.html

JSONArray和JSONObject参考如下资料:http://www.cnblogs.com/xwdreamer/archive/2011/12/16/2296904.html

android判断网络是否连接,参考http://www.cnblogs.com/qingblog/archive/2012/07/19/2598983.html

原文地址:https://www.cnblogs.com/Shirlies/p/3050072.html