android twitter 4j integration get tweet entities

谁都会走 提交于 2019-12-10 15:39:48

问题


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;
    }

回答1:


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());
                    }
}


来源:https://stackoverflow.com/questions/9834787/android-twitter-4j-integration-get-tweet-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!