I am able to fetch the last tweet by a particular user and place that in my Android layout. However, I need to fetch all tweets by that user and display them in the layout.
Here is my code so far:
httpClient = new DefaultHttpClient();
tweetList = (TextView) findViewById(R.id.tvTweet);
new ReadJSON().execute("text");
public org.json.JSONObject alltweets(String username) throws ClientProtocolException, IOException, JSONException {
StringBuilder sb = new StringBuilder(URL);
sb.append(username);
HttpGet get = new HttpGet(sb.toString());
HttpResponse response = httpClient.execute(get);
int statuss = response.getStatusLine().getStatusCode();
if (statuss == 200) {
int i;
HttpEntity e = response.getEntity();
String data = EntityUtils.toString(e);
JSONArray timeline = new JSONArray(data);
for (i=0; i alltweets = timeline.getJSONObject(i);
Log.i("tweet", timeline.getJSONObject(i) + "");
}
return alltweets;
} else {
return null;
}
}
public class ReadJSON extends AsyncTask {
@Override
protected String doInBackground(String...params) {
// TODO Auto-generated method stub
try {
json = alltweets("username"); //particular user name from which page you want to fetch tweets
return json.getString("text");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
Log.i("tweet is ", result);
tweetList.setText(result);
}
}
No comments:
Post a Comment