android - How to use the twitter4j lib to get the tweets of screen name? -


i have seen lot of tutorials using lib dint clear idea of it.

firstly how can authenticate twitter app??,

is there way can hardcode access token, user does'nt have can directly search particular user's tweet entering screen name??

how can tweets after mentioning screen name??

i tried reading docs twitter4j lib dint me....

i need im stuck in 2 days , plz help...

there multiple ways authenticate:

first of need create application here. receive consumer key , secret:

consumer key

you can use code request authorization @ start up.

public class mainactivity extends activity {      // twitterproperties     private commonshttpoauthconsumer httpoauthconsumer;     private oauthprovider httpoauthprovider;      public final static string consumerkey = "your consumer key";     public final static string consumersecret = "your consumer secret";      private final string callbackurl = "scheme://host";      private twitter twitter;     accesstoken a;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          strictmode.enabledefaults();         requestwindowfeature(window.feature_no_title);         getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,                 windowmanager.layoutparams.flag_fullscreen);         setcontentview(r.layout.activity_main);          doauth();     }      private void doauth() {         try {             httpoauthconsumer = new commonshttpoauthconsumer(consumerkey,                     consumersecret);             httpoauthprovider = new defaultoauthprovider(                     "https://twitter.com/oauth/request_token",                     "https://twitter.com/oauth/access_token",                     "https://twitter.com/oauth/authorize");             string authurl = httpoauthprovider.retrieverequesttoken(                     httpoauthconsumer, callbackurl);              this.startactivity(new intent(intent.action_view, uri                     .parse(authurl)));         } catch (exception e) {             toast.maketext(this, e.getmessage(), toast.length_long).show();         }     }       @override     protected void onnewintent(intent intent) {         super.onnewintent(intent);          uri uri = intent.getdata();         if (uri != null && uri.tostring().startswith(callbackurl)) {              string verifier = uri                     .getqueryparameter(oauth.signpost.oauth.oauth_verifier);              // populate token , token_secret in consumer             try {                 httpoauthprovider.retrieveaccesstoken(httpoauthconsumer,                         verifier);             } catch (oauthmessagesignerexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             } catch (oauthnotauthorizedexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             } catch (oauthexpectationfailedexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             } catch (oauthcommunicationexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }               //important part sets authorization can use             = new accesstoken(httpoauthconsumer.gettoken(),                     httpoauthconsumer.gettokensecret());             twitter = new twitterfactory().getinstance();             twitter.setoauthconsumer(consumerkey, consumersecret);             twitter.setoauthaccesstoken(a);         }     }       @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.activity_main, menu);         return true;     } } 

to make work need make couple adjustments manifest.

  • give permission use internet:
    <uses-permission android:name="android.permission.internet" /> 
  • set launch mode singleinstance
    <activity     android:name="com.example.eredivisietwitter.mainactivity"     android:label="@string/app_name"     android:launchmode="singleinstance" > 
  • add intent-filter
<intent-filter>     <action android:name="android.intent.action.view" >     </action>      <category android:name="android.intent.category.default" >     </category>      <category android:name="android.intent.category.browsable" >     </category>      <data         android:host="host"         android:scheme="scheme" >     </data> </intent-filter> 

make sure have same host , scheme in activity:

private final string callbackurl = "scheme://host"; 

now have authorized app can use twitter object request timelines etc.

example:

private void gettweets(string user) {      try {         list<status> statuses;         statuses = twitter.getusertimeline(user);          system.out.println("showing @" + user + "'s user timeline.");         (status status : statuses) {              system.out.println("@" + status.getuser().getscreenname()                     + " - " + status.gettext());         }      } catch (twitterexception te) {         te.printstacktrace();         system.out.println("failed timeline: " + te.getmessage());     }  } 

voilá!


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -