Question

Started coding up a simple android app. Part of it pulls the users last 20 tweets and saves them to a database. When I run this, statuses = twitter.getUserTimeline(); throws an exception its handled and prints out: failed to get timeline-permission denied. When I run the same code non-android, it works fine. I'm a little bit baffled. Could android be limiting the network connection? Im using some database helper classes, haven't included the code because the exception is thrown before any of these are even involved :(Heres the code:

import twitter4j.Paging;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;


import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class  TwitterTest extends ListActivity{
 private ArrayList<String> queryString;
 private DatabaseQuery query;






 /* returns date in string format-without time */
 String getThisYearDate(Date d){
  String x = d.toString();
  String y= x.substring(0, 11);
  String z =x.substring(17);
  String a = y.concat(z);
  return a;
 }

 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  query = new DatabaseQuery(this);

  /*If file exists then tables in db created successfully*/

  File file = new File("exists.txt");




  boolean success = file.exists();

  if (success==false) {
   try {

    ArrayList<Status> statuses = new ArrayList<Status>();


    Twitter twitter = new TwitterFactory().getInstance("USERNAME", "PASSWORD");



    statuses = twitter.getUserTimeline();

    /* adds status data to database*/
    for(Status s: statuses){

     query.appendData("Text", s.getText());
     query.appendData("Date", this.getThisYearDate(s.getCreatedAt()));
     query.appendData("ID",String.valueOf(s.getId()) );
     query.addRow();
    }



   }
   catch (TwitterException te) {
    System.out.println("Failed to get timeline: " + te.getMessage());

    System.exit( -1);
   }
   try{
    boolean created = file.createNewFile();
   }
   catch (IOException e) {

    System.out.println("error");




   }

   queryString = query.getData(new String[] {"Text"},this.getThisYearDate(new Date()), null, null, null, "Date", " ASC");
   try {
    query.destroy();
   } catch (Throwable e) {
    e.printStackTrace();
   }

  } else {
   // File already exists
   queryString = query.getData(new String[] {"Text"},this.getThisYearDate(new Date()), null, null, null, "Date", " ASC");
   try {
    query.destroy();
   } catch (Throwable e) {
    e.printStackTrace();
   }

  }





 // Set the ListView
 setListAdapter(new ArrayAdapter<String>(this,
   android.R.layout.simple_list_item_1, queryString));
 getListView().setTextFilterEnabled(true);
}
}
Was it helpful?

Solution

Sounds like you have not added the permission android.permission.INTERNET into your AndroidManifest.xml.

OTHER TIPS

Use asynchronous task ,It worked for me

public class Asy  extends AsyncTask<String, String, String> { 
static String msg ;
@Override
protected void onPreExecute() {
    super.onPreExecute();

}


protected String doInBackground(String... args) {

   android.tweet(msg);
    return null;
}


protected void onPostExecute(String file_url) {


}
}

where android is a class that contains your code :

public class android {
static boolean got = true;
public static String consumerKey = "************";

public static String consumerSecret = "***********";

public final static String accesstoken = "***************";

public final static String accesstokensecret = "**************";

private final String CALLBACKURL = "T4J_OAuth://callback_main";

public static void tweet(String s) {

    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthAccessToken(accesstoken);
    builder.setOAuthAccessTokenSecret(accesstokensecret);
    builder.setOAuthConsumerKey(consumerKey);
    builder.setOAuthConsumerSecret(consumerSecret);
    OAuthAuthorization auth = new OAuthAuthorization(builder.build());
    Twitter twitter = new TwitterFactory().getInstance(auth);

    try {
twitter.updateStatus(s);       //I was trying to tweet
        //put whatever you want to do
    } catch (TwitterException e) {

        return;
    }

}

then call this asynchronous task using this code :

Asy.msg = "whatever";      // the status I was posting
Asy task3 = new Asy();
task3.execute();`

It worked for me . Good Luck

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top