编程语言
首页 > 编程语言> > java – android twitter 4j集成获取推文实体

java – android twitter 4j集成获取推文实体

作者:互联网

我正在使用Twiitter4j与android集成android.我正在尝试获取Home时间轴及其工作正常.但是,当我希望在推文中包含网址时,没有任何功能.是否有一个功能,以获得像网址等推文.

这是代码

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

适用于索尼的代码,之后我将代码移动到异步任务,并显示一些错误

这是我编辑的代码

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

解决方法:

您只需要在创建ConfigurationBuilder的对象时再添加一行

cb.setIncludeEntitiesEnabled(真);

(提示下一个代码)然后你会获得实体,

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

标签:java,android,twitter,twitter4j
来源: https://codeday.me/bug/20190704/1376797.html