我想在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
答案 0 :(得分:0)
确保您在AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
要显示Twitter Feed,请按照以下教程进行操作:
http://www.youtube.com/watch?v=TxcfhAU2dDg&list=SP2F07DBCDCC01493A&index=147
答案 1 :(得分:0)
In view u haven't set the textview.So, Might this help you
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
Holder holder=new Holder();
if (view == null) {
LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.tweet_item, null);
}
Tweet tweet = tweets.get(position);
if (tweet != null) {
holder.username = (TextView) view.findViewById(R.id.listItem);
holder.message = (TextView) view.findViewById(R.id.message);
if (username != null) {
username.setText(tweet.username);
}
if(message != null) {
message.setText(tweet.message);
}
}
view.setTag(holder);
return view;
}
}
class Holder{
TextView username;
TextView message;
}
答案 2 :(得分:0)
可以使用“SearchTimeline”在他/她的Android应用程序中显示推文:请参考以下链接:
https://docs.fabric.io/android/twitter/show-timelines.html#timelines