我试图从Twitter4j中的状态对象获得OEmbed对象,然后将html传递到网页。现在我只把它放在测试的主方法中。
public static void main(String[] args) {
ConfigurationBuilder cf = new ConfigurationBuilder();
cf.setDebugEnabled(true)
.setOAuthConsumerKey("")
.setOAuthConsumerSecret("")
.setOAuthAccessToken("")
.setOAuthAccessTokenSecret("")
.setJSONStoreEnabled(true);
TwitterFactory tf = new TwitterFactory(cf.build());
Twitter twitter = tf.getInstance();
Query query = new Query("hello world");
query.setCount(5);
try {
QueryResult result = twitter.search(query);
List<Status> tweets = result.getTweets();
String jsonString = TwitterObjectFactory.getRawJSON(tweets.get(0));
System.out.println(jsonString);
OEmbed embed = TwitterObjectFactory.createOEmbed(jsonString);
System.out.println(embed.getHtml());
} catch(TwitterException e) {
e.printStackTrace();
}
}
凭据已被删除,但它们在程序中是正确的。控制台输出如下:
{"metadata":{"result_type":"recent","iso_language_code":"en"},"in_reply_to_status_id_str":null,"in_reply_to_status_id":null,"coordinates":null,"created_at":"Mon Sep 05 01:33:28 +0000 2016","truncated":false,"in_reply_to_user_id_str":null,"source":"<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>","retweet_count":0,"retweeted":false,"geo":null,"in_reply_to_screen_name":null,"is_quote_status":false,"entities":{"urls":[],"hashtags":[],"user_mentions":[],"symbols":[]},"id_str":"772608554834341888","in_reply_to_user_id":null,"favorite_count":0,"id":772608554834341888,"text":"Neville is the most adorable boy in the world","place":{"country_code":"US","country":"United States","contained_within":[],"full_name":"Siloam Springs, AR","bounding_box":{"coordinates":[[[-94.569269,36.15253],[-94.487657,36.15253],[-94.487657,36.2142221],[-94.569269,36.2142221]]],"type":"Polygon"},"place_type":"city","name":"Siloam Springs","attributes":{},"id":"86abfc720d360e37","url":"https://api.twitter.com/1.1/geo/id/86abfc720d360e37.json"},"contributors":null,"lang":"en","user":{"utc_offset":null,"friends_count":258,"profile_image_url_https":"https://pbs.twimg.com/profile_images/767399989085483008/Xm0kzU-V_normal.jpg","listed_count":5,"profile_background_image_url":"http://abs.twimg.com/images/themes/theme1/bg.png","default_profile_image":false,"favourites_count":13032,"description":"Jesus. Dreams. World Domination Schemes.","created_at":"Wed Feb 26 23:03:16 +0000 2014","is_translator":false,"profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme1/bg.png","protected":false,"screen_name":"hello_lovelyys","id_str":"2363303335","profile_link_color":"0084B4","is_translation_enabled":false,"id":2363303335,"geo_enabled":true,"profile_background_color":"C0DEED","lang":"en","has_extended_profile":false,"profile_sidebar_border_color":"C0DEED","profile_text_color":"333333","verified":false,"profile_image_url":"http://pbs.twimg.com/profile_images/767399989085483008/Xm0kzU-V_normal.jpg","time_zone":null,"url":null,"contributors_enabled":false,"profile_background_tile":false,"profile_banner_url":"https://pbs.twimg.com/profile_banners/2363303335/1471797391","entities":{"description":{"urls":[]}},"statuses_count":2760,"follow_request_sent":false,"followers_count":167,"profile_use_background_image":true,"default_profile":true,"following":false,"name":"a stray moonbeam","location":"Strolling through Mordor","profile_sidebar_fill_color":"DDEEF6","notifications":false},"favorited":false}
我肯定得到原始JSON字符串,但由于某种原因createOEmbed方法不喜欢它。下面是堆栈跟踪。
JSONObject["html"] not found.
Relevant discussions can be found on the Internet at:
http://www.google.co.jp/search?q=e6edd861 or
http://www.google.co.jp/search?q=0003ff21
TwitterException{exceptionCode=[e6edd861-0003ff21 7fe5ced0-0f00c8c2], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=4.0.4}
at twitter4j.OEmbedJSONImpl.init(OEmbedJSONImpl.java:64)
at twitter4j.OEmbedJSONImpl.<init>(OEmbedJSONImpl.java:46)
at twitter4j.TwitterObjectFactory.createOEmbed(TwitterObjectFactory.java:271)
at com.omnom.connections.TwitterConn.main(TwitterConn.java:53)
Caused by: twitter4j.JSONException: JSONObject["html"] not found.
at twitter4j.JSONObject.get(JSONObject.java:391)
at twitter4j.JSONObject.getString(JSONObject.java:505)
at twitter4j.OEmbedJSONImpl.init(OEmbedJSONImpl.java:51)
... 3 more
感谢阅读!
在尝试获取tweet的html之前,必须使用oembeddrequest Object。
的例子:
String url = ""
//gets url from id of tweet
OEmbedRequest oEmbedRequest = new OEmbedRequest("tweet id goes here", url);
//embeds into html code to be viewed
OEmbed embed = twitter.getOEmbed(oEmbedRequest);
//html result code
System.out.print(embed.getHtml());