Android 用 JSON 讀取 Twitter

基本上是用一個Loop把文定寫入ArrayList
再用 ListView 顯示

 public ArrayList<String> FetchTwitterPublicTimeline(String target)  
   {  
        ArrayList<String> listItems = new ArrayList<String>();  
        try {  
                URL twitter = new URL(  
                          "http://twitter.com/statuses/public_timeline.json");  
                URLConnection urlConn = twitter.openConnection();  
                BufferedReader in = new BufferedReader(new InputStreamReader(  
                          urlConn.getInputStream()));  
                String line;  
                while ((line = in.readLine()) != null) {  
                     JSONArray jsonArray = new JSONArray(line);  
                     for (int i = 0; i < jsonArray.length(); i++) {  
                          JSONObject jsonObj = (JSONObject) jsonArray.get(i);  
                          listItems.add(jsonObj.getString(target));  
                     }  
                }  
           } catch (MalformedURLException e) {  
                e.printStackTrace();  
           } catch (IOException e) {  
                e.printStackTrace();  
           } catch (JSONException e) {  
                e.printStackTrace();  
           }  
           return listItems;  
   }  




沒有留言:

張貼留言