0

我想在 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 
4

3 回答 3

0

确保您拥有互联网许可AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" /> 

要显示 twitter 提要,请遵循本教程:

http://www.youtube.com/watch?v=TxcfhAU2dDg&list=SP2F07DBCDCC01493A&index=147

于 2013-08-27T08:42:00.407 回答
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;
}
于 2013-08-27T09:22:26.460 回答
0

可以简单地使用“SearchTimeline”在他/她的 android 应用程序中显示推文:请参考以下链接:

https://docs.fabric.io/android/twitter/show-timelines.html#timelines

于 2016-04-14T07:56:36.573 回答