Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. package com.example;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import org.apache.http.client.HttpClient;
  6. import org.apache.http.client.ResponseHandler;
  7. import org.apache.http.client.methods.HttpGet;
  8. import org.apache.http.impl.client.BasicResponseHandler;
  9. import org.apache.http.impl.client.DefaultHttpClient;
  10. import org.json.simple.JSONArray;
  11. import org.json.simple.JSONObject;
  12. import org.json.simple.parser.JSONParser;
  13.  
  14. import android.app.ListActivity;
  15. import android.graphics.Bitmap;
  16. import android.os.Bundle;
  17. import android.util.Log;
  18. import android.view.View;
  19. import android.widget.ListView;
  20. import android.widget.Toast;
  21.  
  22. public class Example extends ListActivity {
  23.  
  24.     public Bitmap placeholder;
  25.    
  26.     @Override
  27.     public void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.         setContentView(R.layout.main);
  30.        
  31.         ArrayList<Tweet> tweets = getTweets("android", 1);
  32.        
  33.         ListView listView = (ListView) findViewById(R.id.ListViewId);
  34.         listView.setAdapter(new TweetItemAdapter(this, R.layout.listitem, tweets));        
  35.     }
  36.  
  37.     protected void onListItemClick(ListView l, View v, int position, long id) {
  38.         // process click on item #position
  39.         Tweet item = (Tweet) l.getItemAtPosition(position);
  40.         Log.v("position", item.toString());
  41.     }
  42.  
  43.     public ArrayList<Tweet> getTweets(String searchTerm, int page) {
  44.         String searchUrl = "http://search.twitter.com/search.json?q=@"
  45.                             + searchTerm + "&rpp=100&page=" + page;
  46.        
  47.         ArrayList<Tweet> tweets = new ArrayList<Tweet>();
  48.        
  49.         HttpClient client = new  DefaultHttpClient();
  50.         HttpGet get = new HttpGet(searchUrl);
  51.          
  52.         ResponseHandler<String> responseHandler = new BasicResponseHandler();
  53.  
  54.         String responseBody = null;
  55.         try{
  56.             responseBody = client.execute(get, responseHandler);
  57.         }catch(Exception ex) {
  58.             ex.printStackTrace();
  59.         }
  60.  
  61.         JSONObject jsonObject = null;
  62.         JSONParser parser=new JSONParser();
  63.        
  64.         try {
  65.             Object obj = parser.parse(responseBody);
  66.             jsonObject=(JSONObject)obj;
  67.            
  68.         }catch(Exception ex){
  69.             Log.v("TEST","Exception: " + ex.getMessage());
  70.         }
  71.        
  72.         JSONArray arr = null;
  73.        
  74.         try {
  75.             Object j = jsonObject.get("results");
  76.             arr = (JSONArray)j;
  77.         }catch(Exception ex){
  78.             Log.v("TEST","Exception: " + ex.getMessage());
  79.         }
  80.  
  81.         for(Object t : arr) {
  82.             Tweet tweet = new Tweet(
  83.                     ((JSONObject)t).get("from_user").toString(),
  84.                     ((JSONObject)t).get("text").toString(),
  85.                     ((JSONObject)t).get("profile_image_url").toString()
  86.                     );
  87.             tweets.add(tweet);
  88.         }
  89.        
  90.         return tweets;
  91.     }  
  92.  
  93.     /** Classes **/
  94.    
  95.     public class Tweet {
  96.         public String username;
  97.         public String message;
  98.         public String image_url;
  99.         public Boolean usernameSet = false;
  100.         public Boolean messageSet = false;
  101.         public Boolean imageSet = false;
  102.         public Bitmap avatar;
  103.        
  104.         public Tweet(String username, String message, String url) {
  105.             this.username = username;
  106.             this.message = message;
  107.             this.image_url = url;
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement