Search code examples
javaandroidtwittertwitter4j

android twitter 4j integration get tweet entities


I am working on twitter integration with android using Twiitter4j. I am trying to fetch Home timelines and its working fine. But when i am looking to get urls included in the tweets there is no functions. Is there a functon to get tweet entites like urls etc.

Here is the code

cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(Constants.CONSUMER_KEY)
            .setOAuthConsumerSecret(Constants.CONSUMER_SECRET)
            .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(
                    accessSecret);

    try {
        TwitterFactory factory = new TwitterFactory(cb.build());
        // gets Twitter instance with default credentials
        Twitter twitter = factory.getInstance();
        User user = twitter.verifyCredentials();
        List<Status> statuses = twitter.getHomeTimeline();
        System.out.println("Showing @" + user.getScreenName() + "'s home timeline.");
        for (Status status : statuses) {

            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + "--"+status.getURLs()+ "--"+status.getURLEntities());
        }
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get timeline: " + te.getMessage());
        System.exit(-1);
    }

Works well with Sony's code and after that i have moved my code to async task and it shows some errors

Here is my edited code

        protected String doInBackground(Void... args) {
        String response = null;

        try {
            TwitterFactory factory = new TwitterFactory(cb.build());
            // gets Twitter instance with default credentials
            Twitter twitter = factory.getInstance();
            User user = twitter.verifyCredentials();
            ResponseList<Status> statuses = twitter.getHomeTimeline();


            System.out.println("Showing @" + user.getScreenName() + "'s home timeline.");
            for (Status status : statuses) {

                System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + "--"+status.getUser().getProfileImageURL());
                URLEntity[] uent = status.getURLEntities();
                if (uent != null) {
                    for (int k = 0; k < uent.length; k++) {
                        Log.i("URL Entity", "Dp Url " + uent[k].getDisplayURL()
                                + " URL " + uent[k].getURL() + " start "
                                + uent[k].getStart() + " end "
                                + uent[k].getEnd());
                    }
}
            }

        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to get timeline: " + te.getMessage());
            System.exit(-1);
        }
        return response;
    }

Solution

  • You just need to add one more line while creating object of ConfigurationBuilder

    cb.setIncludeEntitiesEnabled(true);

    (Hint for the next code)And you will get the Entities then,

    ResponseList<Status> list = twitter.getHomeTimeline();
    URLEntity[] uent = list.get(0).getURLEntities();
    if (uent != null) {
                        for (int k = 0; k < uent.length; k++) {
                            Log.i("URL Entity", "Dp Url " + uent[k].getDisplayURL()
                                    + " URL " + uent[k].getURL() + " start "
                                    + uent[k].getStart() + " end "
                                    + uent[k].getEnd());
                        }
    }