Advertisement
yudiwbs

JSON parsing untuk Tweet

May 14th, 2012
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.33 KB | None | 0 0
  1. //lib JSON didownload di: http://code.google.com/p/json-simple/
  2. //cara memasukkan lib ke project android: http://stackoverflow.com/questions/3642928/adding-a-library-jar-to-an-eclipse-android-project
  3.  
  4. public ArrayList<TweetData> parser(String resBody) throws Exception {
  5.         ArrayList<TweetData> alTweet = new ArrayList<TweetData>();
  6.         JSONObject jsonObject = null;
  7.         JSONParser parser=new JSONParser();
  8.         try {
  9.             Object obj = parser.parse(resBody);
  10.             jsonObject=(JSONObject)obj;
  11.           }catch(Exception ex){
  12.             Log.e("yw","Exception parser: " + ex.getMessage());
  13.           }
  14.           JSONArray arr = null;
  15.           try {
  16.             Object j = jsonObject.get("results");
  17.             arr = (JSONArray)j;
  18.           } catch(Exception ex){
  19.             Log.e("yw","Exception ambil result tweets: " + ex.getMessage());
  20.           }
  21.          
  22.           if (arr!=null) {
  23.               for(Object t : arr) {
  24.                     TweetData td=null;
  25.                     try {
  26.                         td = new TweetData(
  27.                           ((JSONObject)t).get("from_user").toString(),
  28.                           ((JSONObject)t).get("text").toString(),
  29.                           ((JSONObject)t).get("created_at").toString(),""
  30.                         );
  31.                     } catch (ParseException e) {
  32.                         Log.e("yw","Exception ambil detil tweet: " + e.getMessage());
  33.                     }
  34.                     alTweet.add(td);
  35.               }
  36.           } else
  37.           {
  38.               Log.e("yw","kosong: "+resBody);
  39.               throw new Exception();
  40.           }
  41.        return alTweet;
  42.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement