gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:28:12 24 4
gpt4 key购买 nike

我正在使用 Twiitter4j 将 Twitter 与 Android 集成。我正在尝试获取主页时间表及其工作正常。但是当我希望在推文中包含 url 时,没有任何功能。是否有获取 url 等推文实体的功能。

这是代码

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

与 Sony 的代码配合良好,之后我将我的代码移至异步任务并显示一些错误

这是我修改后的代码

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

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

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 4j 集成获取推文实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9834787/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com