我想在 android 应用程序的列表视图中显示推文。我使用了以下代码,但推文没有显示.. 谁能告诉如何在列表视图中显示 5 条最新推文,每 15 分钟重新加载一次?我尝试了很多代码,但没有达到任何结果!
public class TweetsListActivity extends Activity {
TextView tv;
ListView lv;
Twitter twitter;
TextView username;
TweetsListAdapter adapter;
ArrayList<Tweet> tweetsDataList;
String tweetsData;
static ConfigurationBuilder cb;
private static SharedPreferences mSharedPreferences;
AlertDialogManager alert = new AlertDialogManager();
private static final String CONSUMER_KEY = "";
private static final String CONSUMER_SECRET = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.tweets_list);
super.onCreate(savedInstanceState);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
tv = (TextView) findViewById(R.id.textView1);
username =(TextView)findViewById(R.id.username);
lv = (ListView) findViewById(R.id.listView);
tweetsDataList = new ArrayList<Tweet>();
String responseBody = null;
尝试 {
ConfigurationBuilder builder = new ConfigurationBuilder(); builder.setOAuthConsumerKey(CONSUMER_KEY); builder.setOAuthConsumerSecret(CONSUMER_SECRET);
URI website = new URI("https://api.twitter.com/1/statuses/user_timeline/.json");
HttpClient hc =new DefaultHttpClient();
HttpGet get = new HttpGet();
get.setURI(website);
HttpResponse rp = hc.execute(get);
HttpEntity entity = rp.getEntity();
responseBody = EntityUtils.toString(entity);
if(((org.apache.http.HttpResponse)rp).getStatusLine().getStatusCode() == HttpStatus.SC_OK){
String returned = null;
JSONObject json = new JSONObject(returned);
JSONArray ja = null;
ja = json.getJSONArray("results");
for (int i1 = 0; i1 < ja.length(5); i1++) {
JSONObject tweet_obj = ja.getJSONObject(i1);
Tweet tweetsData = new Tweet();
String username = tweet_obj.getString("from_user");
String message = tweet_obj.getString("text"); tweetsDataList.add(tweetsData);
}
}
}
catch (Exception e) {
Log.e("TwitterFeedActivity", "Error loading JSON", e);
}
adapter = new TweetsListAdapter(TweetsListActivity.this, tweetsDataList);
lv.setAdapter(adapter);
mSharedPreferences=getApplicationContext().getSharedPreferences("MyPref",0);
}
}
}
but no list is showing .. neither the logcat shows any error..
i have used adapter classes. here is the link http://pastebin.com/XBErcu9c