I am trying to retrieve status with Twitter4j, but I get a Unfortunately TestProject has stopped error and my app closes. I am also not getting anything in logcat so I do not know where to start troubleshooting.
This is the method executed when a button is pressed, I am basically making an instance of the class which contains all the oAuth setup and the call to the twitter API for the status. I know this should probably be done with an Async call since it deals with network, but for now I just wanted to retrieve a status even if the UI is blocked:
public void onGetStatus(View v){
if(v.getId()==R.id.button1){
GetUserStatus status = new GetUserStatus();
ResponseList<Status> a = status.twitterSettings();
for(Status s: a){
editText.setText(s.getText());
}
}
}
This is the class with all the twitter configurations for my account.
public class GetUserStatus {
public ResponseList<Status> twitterSettings(){
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey("xxxxxx");
cb.setOAuthConsumerSecret("xxxx");
cb.setOAuthAccessToken("xxxxx");
cb.setOAuthAccessTokenSecret("xxxx");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
try {
ResponseList<Status> a = twitter.getUserTimeline(new Paging(1,5));
return a;
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
Research I have done
I have been mostly going off twitter4j's website code examples and it seems everything is up to date so I dont think twitter4j is deprecated. I have also looked through other twitter4j questions on StackOverflow.com but most deal with 404 error or some other type of error, while I am not getting any errors.
The issue was StrictMode.ThreadPolicy was introduced since API Level 9 and the default thread policy had been changed since API Level 11, which in short, does not allow network operation
(eg: HttpClient and HttpUrlConnection) get executed on UI thread. If you do this, you get NetworkOnMainThreadException.
Using AsyncTask fixed the issue. I added a nested class within my activity
class GetUserStatus extends AsyncTask < Void, Void, ResponseList < twitter4j.Status >> {
@
Override
protected ResponseList < twitter4j.Status > doInBackground(Void...params) {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey("XXXX");
cb.setOAuthConsumerSecret("XXXXX");
cb.setOAuthAccessToken("XXXXX");
cb.setOAuthAccessTokenSecret("XXXXXXX");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
try {
ResponseList < twitter4j.Status > a = twitter.getUserTimeline(new Paging(1, 5));
return a;
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(ResponseList < twitter4j.Status > stats) {
for (twitter4j.Status a: stats) {
editText.setText(a.getText());
}
}
}
The other issue I was having with logcat not displaying exceptions, was because Eclipse automatically selected a filter, when I chose the option "all messages (no filter)" I was able to see the stack trace.