java - Android Twitter App Can't Make Objects from Json Response -


i'm trying make objects out of twitter stream download user. using information provided https://github.com/rockncoder/twittertutorial. can determine if code works? of classes kind of sketchy, in twitter.java class arraylist , has what's listed below in it.

is process correct? appreciated.

public class mainactivity extends listactivity {

private listactivity activity; final static string screenname = "riddlemetombers"; final static string log_tag = "rmt";  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      activity = this;      downloadtweets();  }   // download twitter timeline after first checking see if there network connection public void downloadtweets() {     connectivitymanager connmgr = (connectivitymanager) getsystemservice(context.connectivity_service);     networkinfo networkinfo = connmgr.getactivenetworkinfo();      if (networkinfo != null && networkinfo.isconnected()) {         new downloadtwittertask().execute(screenname);     } else {         log.v(log_tag, "no network connection available.");     } }   // uses asynctask download twitter user's timeline private class downloadtwittertask extends asynctask<string, void, string> {     final string consumer_key = (string) getresources().getstring(r.string.api_key);     final string consumer_secret = (string)getresources().getstring(r.string.api_secret);     final static string twittertokenurl = "https://api.twitter.com/oauth2/token";     final static string twitterstreamurl = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=";      @override     protected string doinbackground(string... screennames) {         string result = null;          if (screennames.length > 0) {             result = gettwitterstream(screennames[0]);         }         return result;     }      // onpostexecute convert json results twitter object (which array list of tweets     @override     protected void onpostexecute(string result) {         twitter twits = jsontotwitter(result);          // lets write results console         (tweet tweet : twits) {             log.i(log_tag, tweet.gettext());         }          // send tweets adapter rendering         arrayadapter<tweet> adapter = new arrayadapter<tweet>(activity, r.layout.items, twits);         setlistadapter(adapter);     }      // converts string of json data twitter object     private twitter jsontotwitter(string result) {         twitter twits = null;         if (result != null && result.length() > 0) {             try {                 gson gson = new gson();                 twits = gson.fromjson(result, twitter.class);                 if(twits==null){log.d(log_tag, "twits null");}                 else if(twits!=null) {log.d(log_tag, "twits not null");}             } catch (illegalstateexception ex) {                 // eat exception             }         }         return twits;     }      // convert json authentication object authenticated object     private authenticated jsontoauthenticated(string rawauthorization) {         authenticated auth = null;         if (rawauthorization != null && rawauthorization.length() > 0) {             try {                 gson gson = new gson();                 auth = gson.fromjson(rawauthorization, authenticated.class);             } catch (illegalstateexception ex) {                 // eat exception             }         }         return auth;     }      private string getresponsebody(httprequestbase request) {         stringbuilder sb = new stringbuilder();         try {              defaulthttpclient httpclient = new defaulthttpclient(new basichttpparams());             httpresponse response = httpclient.execute(request);             int statuscode = response.getstatusline().getstatuscode();             string reason = response.getstatusline().getreasonphrase();              if (statuscode == 200) {                  httpentity entity = response.getentity();                 inputstream inputstream = entity.getcontent();                  bufferedreader breader = new bufferedreader(new inputstreamreader(inputstream, "utf-8"), 8);                 string line = null;                 while ((line = breader.readline()) != null) {                     sb.append(line);                 }             } else {                 sb.append(reason);             }         } catch (unsupportedencodingexception ex) {         } catch (clientprotocolexception ex1) {         } catch (ioexception ex2) {         }         return sb.tostring();     }      private string gettwitterstream(string screenname) {         string results = null;          // step 1: encode consumer key , secret         try {             // url encode consumer key , secret             string urlapikey = urlencoder.encode(consumer_key, "utf-8");             string urlapisecret = urlencoder.encode(consumer_secret, "utf-8");              // concatenate encoded consumer key, colon character, ,             // encoded consumer secret             string combined = urlapikey + ":" + urlapisecret;              // base64 encode string             string base64encoded = base64.encodetostring(combined.getbytes(), base64.no_wrap);              // step 2: obtain bearer token             httppost httppost = new httppost(twittertokenurl);             httppost.setheader("authorization", "basic " + base64encoded);             httppost.setheader("content-type", "application/x-www-form-urlencoded;charset=utf-8");             httppost.setentity(new stringentity("grant_type=client_credentials"));             string rawauthorization = getresponsebody(httppost);             authenticated auth = jsontoauthenticated(rawauthorization);              // applications should verify value associated             // token_type key of returned object bearer             if (auth != null && auth.token_type.equals("bearer")) {                  // step 3: authenticate api requests bearer token                 httpget httpget = new httpget(twitterstreamurl + screenname);                  // construct normal https request , include authorization                 // header value of bearer <>                 httpget.setheader("authorization", "bearer " + auth.access_token);                 httpget.setheader("content-type", "application/json");                 // update results body of response                 results = getresponsebody(httpget);             }         } catch (unsupportedencodingexception ex) {         } catch (illegalstateexception ex1) {         }         return results;     } }       @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; }  } 

twitter class

import java.util.arraylist;  // collection of tweets public class twitter extends arraylist<tweet> {     private static final long serialversionuid = 1l; } 

tweet class

import com.google.gson.annotations.serializedname;  public class tweet {  @serializedname("created_at") private string datecreated;  @serializedname("id") private string id;  @serializedname("text") private string text;  @serializedname("in_reply_to_status_id") private string inreplytostatusid;  @serializedname("in_reply_to_user_id") private string inreplytouserid;  @serializedname("in_reply_to_screen_name") private string inreplytoscreenname;  @serializedname("user") private twitteruser user;  public string getdatecreated() {     return datecreated; }  public string getid() {     return id; }  public string getinreplytoscreenname() {     return inreplytoscreenname; }  public string getinreplytostatusid() {     return inreplytostatusid; }  public string getinreplytouserid() {     return inreplytouserid; }  public string gettext() {     return text; }  public void setdatecreated(string datecreated) {     datecreated = datecreated; }  public void setid(string id) {     id = id; }  public void setinreplytoscreenname(string inreplytoscreenname) {     inreplytoscreenname = inreplytoscreenname; }  public void setinreplytostatusid(string inreplytostatusid) {     inreplytostatusid = inreplytostatusid; }  public void setinreplytouserid(string inreplytouserid) {     inreplytouserid = inreplytouserid; }  public void settext(string text) {     text = text; }  public void setuser(twitteruser user) {     user = user; }  public twitteruser getuser() {     return user; }  @override public string  tostring(){     return gettext(); } } 

i've done several log.d(log_tag, stuff) see if i'm getting stuff, , indicates i'm getting kind of content back. maybe problem in making objects of data.

not sure why want use code https://github.com/rockncoder/twittertutorial.

why don't use use http://twitter4j.org. have give sample example use it.

moreover support twitter 1.1 well. include twitter-core.jar , ready write code.

hope helps.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -