Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to show tweets in a list view in android application.i have used following code but the tweets are not showing.. could anybody pls tell how to show 5 latest tweets in list view which are reloaded after every 15 minutes? i have tried many codes but did not reached at any result!

TweetsListActivity.java

public class TweetsListActivity extends ListActivity {
TextView tv;
ListView lv;
RequestToken requestToken;
Twitter twitter;
TextView username;
Textview message;
TweetsListAdapter adapter;
ArrayList<tweet> tweetsDataList;
String tweetsData;

static ConfigurationBuilder cb;
private static SharedPreferences mSharedPreferences;
AlertDialogManager alert = new AlertDialogManager();

@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.tweets_list);
super.onCreate(savedInstanceState);

username = (TextView) findViewById(R.id.username);
message = (TextView) findViewById(R.id.message)
getTweets("android", 1);
lv = (ListView) findViewById(R.id.listView);
adapter = new TweetsListAdapter(TweetsListActivity.this, tweetsDataList);
lv.setAdapter(adapter);
mSharedPreferences = getApplicationContext().getSharedPreferences("MyPref",
0);
}
private ArrayList<tweet> getTweets(String searchTerm, int page) {
Intent i = getIntent();
tweetsData = i.getStringExtra("tweetsData");
Log.v("SizeNext--", "" + tweetsData);
tweetsDataList = new ArrayList<tweet>();

String responseBody = null;
try {
HttpClient hc = (HttpClient) new DefaultHttpClient();
HttpGet get = new HttpGet(
"http://search.twitter.com/search.json?q==@" + searchTerm+ "&rpp=100&page=" + page);
HttpResponse rp = (HttpResponse) ((AbstractHttpClient) hc).execute(get);

if (((org.apache.http.HttpResponse) rp).getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils
.toString(((org.apache.http.HttpResponse) rp).getEntity());

JSONObject json = null;
JSONArray ja = null;
Object obj;
json = (JSONObject) obj;
ja = json.getJSONArray("results");

for (int i1 = 0; i1 < ja.length(); i1++) {
JSONObject tweet_obj = ja.getJSONObject(i1);
Tweet tweetsData = new Tweet();
tweetsData.setId(tweet_obj.getString("id"));
tweetsData.setUsername(tweet_obj.getString("from_user"));
tweetsData.setMessage(tweet_obj.getString("text"));
tweetsDataList.add(tweetsData);
}
}
}
catch (Exception e) {
Log.e("TwitterFeedActivity", "Error loading JSON", e);
}
return tweetsDataList;
}

LOGCAT ERROR

08-28 01:52:35.923: E/TwitterFeedActivity(2383): Error loading JSON
08-28 01:52:35.923: E/TwitterFeedActivity(2383): java.lang.ClassCastException: org.apache.http.impl.client.DefaultHttpClient cannot be cast to twitter4j.internal.http.HttpClient
08-28 01:52:35.923: E/TwitterFeedActivity(2383): at com.twitter_demo_project.TweetsListActivity.getTweets(TweetsListActivity.java:144)
08-28 01:52:35.923: E/TwitterFeedActivity(2383): at com.twitter_demo_project.TweetsListActivity.onCreate(TweetsListActivity.java:74)
08-28 01:52:35.923: E/TwitterFeedActivity(2383): at android.app.Activity.performCreate(Activity.java:5133)

any help would be highly appreciated.. thank you.
Posted
Updated 3-Sep-13 18:10pm
v5
Comments
Amanpreet Mukker 3-Sep-13 15:29pm    
submit the proper formatted code, please. This is hard to read !
Ganesh KP 4-Sep-13 0:34am    
Try to move the network operation to AsyncTask class and in AsyncTask class write the code for getting the tweets from twitter and onPostExecute() update the listview. Some times you are not allowed to do network operations in normal UI thread. Try that and let us know if u r facing any problems.
Member 10239936 4-Sep-13 5:23am    
i have made these changes now. it fetches the list properly but returns only tweets from 'Twitter' ,not my timeline tweets. i think i have to change url for this but don't know which url is needed for this. i have tried to put home_timeline in place of user_timeline,but didn't work. please suggest

protected void onCreate(Bundle savedInstanceState) {
final String tweetsData = getIntent().getStringExtra("tweetsData");
lv = (ListView) findViewById(R.id.listView);
new TweetsTask().execute(tweetsData);
}
private void popularListView(ArrayList<tweet> tweets) {
TweetsArrayAdapter tweetAdapter = new TweetsArrayAdapter(this,R.layout.tweet_item, tweets);
lv.setAdapter(tweetAdapter);
}
public class TweetsTask extends AsyncTask<string,>> {
private ProgressDialog progressDialog;
private static final String URL_BASE = "https://api.twitter.com";
private static final String URL_BUSCA = URL_BASE+ "/1.1/statuses/user_timeline.json?screen_name=";
private static final String URL_AUTH = URL_BASE + "/oauth2/token";

private String autenticateApp() {
HttpURLConnection conn = null;
OutputStream os = null;
BufferedReader br = null;
StringBuilder builder = null;
try {
URL url = new URL(URL_AUTH);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
String credencials = CONSUMER_KEY + ":" + CONSUMER_SECRET;
String autorization = "Basic "+ Base64.encodeToString(credencials.getBytes(), Base64.NO_WRAP);
String parameter = "grant_type=client_credentials";
conn.addRequestProperty("Authorization", autorization);
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
conn.connect();
os = conn.getOutputStream();
os.write(parameter.getBytes());
br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

Log.d(" response POST", String.valueOf(conn.getResponseCode()));
Log.d("Responsed JSON - access token", builder.toString());
}
catch (Exception e) {
Log.e("Error POST", Log.getStackTraceString(e));
}
finally {
if (conn != null) {
conn.disconnect();}}
return builder.toString();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(TweetsListActivity.this);
progressDialog.setMessage("Getting user's tweets");
progressDialog.show();
}
@Override
protected ArrayList<tweet> doInBackground(String... param) {
String tweetsData = param[0];
ArrayList<tweet> tweets = new ArrayList<tweet>();
HttpURLConnection conn = null;
BufferedReader br = null;
try {
URL url = new URL(URL_BUSCA + tweetsData);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
String jsonString = autenticateApp();
JSONObject jsonObj = new JSONObject(jsonString);
String token = jsonObj.getString("token_type") + " "
+ jsonObj.getString("access_token");
conn.setRequestProperty(""Content-Type", "application/json");
conn.conect();
}
Log.d(" response GET", String.valueOf(conn.getResponseCode()));
Log.d("Responsed JSON", response.toString());
JSONArray jsonArray = new JSONArray(response.toString());
JSONObject jsonObject;
for (int i = 0; i < 5; i++) {
jsonObject = (JSONObject) jsonArray.get(i);
Tweet tweet = new Tweet();
tweet.setUsername(jsonObject.getJSONObject("user").getString("name"));
tweet.setMessage(jsonObject.getString("text"));
tweet.setTime(jsonObject.getString("created_at"));
tweets.add(i, tweet);
}
Ganesh KP 6-Sep-13 0:31am    
I think you almost done with your task and only thing is reminding you is the correct url to get the time line tweets. Am I right? @Member 10239936
Member 10239936 6-Sep-13 6:25am    
yeah u r right. i have searched alot but couldn't find correct url that show user's own tweets.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900