java - 使用 Twitter API 从推文中获取坐标
问题描述
我正在尝试使用 Twitter API 和 Java 从推文中正确获取信息。我正在尝试从这条启用了地理标签的推文中获取确切的坐标。
从现在开始,我已经按照文档的要求创建了相应的 Java 类,但也许我不太了解 Geo 对象,因为coordinates
,bounding_box
或都没有place
出现。我的猜测是我没有正确声明这些类。
对于推文类:
// Imports here
public class Tweet {
@SerializedName("created_at")
public String created_at;
@SerializedName("id")
public long id;
// ... All the rest of the parameters
@SerializedName("coordinates")
public Coordinates coordinates;
@SerializedName("place")
public transient Place place;
对于坐标类:
// Imports here
public class Coordinates {
@SerializedName("coordinates")
public String coordinates;
@SerializedName("type")
public String type;
}
对于 Place 类:
// Imports here
public class Place {
@SerializedName("id")
public String id;
@SerializedName("url")
public String url;
// ... All the rest of the parameters
@SerializedName("bounding_box")
public String bounding_box;
}
当我请求从链接的推文中获取 json 信息时,我得到以下数据,并且您可以看到这些字段coordinates
或places
没有出现。如果有用,我将使用 MongoDB Java API 来存储 json 文件及其 Java POJO 管理器。您可以在这里找到所有代码:https ://github.com/marmatsan/theTrainEngine
{
"_id" : NumberLong("1212893878430420992"),
"created_at" : "Fri Jan 03 00:30:09 +0000 2020",
"favorite_count" : 0,
"favorited" : false,
"id_str" : "1212893878430420992",
"is_quote_status" : false,
"lang" : "en",
"possibly_sensitive" : false,
"quoted_status_id" : NumberLong(0),
"quoted_status_id_str" : NumberLong(0),
"reply_count" : 0,
"retweet_count" : 0,
"retweeted" : false,
"source" : "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>",
"text" : "Test 1",
"truncated" : "false",
"user" : {
"_id" : NumberLong(593955589),
"created_at" : "Tue May 29 18:59:38 +0000 2012",
"default_profile" : false,
"default_profile_image" : false,
"description" : "En búsqueda del verdadero camino del Tao estudiando Ingeniería en Tecnologías de Telecomunicación.",
"favourites_count" : 7497,
"followers_count" : 186,
"friends_count" : 114,
"id_str" : "593955589",
"listed_count" : 5,
"location" : "Salamanca/Toledo/Madrid",
"name" : "Martín ",
"profile_banner_url" : "https://pbs.twimg.com/profile_banners/593955589/1513111580",
"profile_image_url_https" : "https://pbs.twimg.com/profile_images/1130587021678927874/50unsAd3_normal.png",
"protected_att" : false,
"screen_name" : "MMateos97",
"statuses_count" : 19420,
"url" : "https://github.com/marmatsan",
"verified" : false
}
}
编辑:如答案中所述,问题在于实现,因为当我transient
从对象中删除修饰符时Place
,程序不起作用。如果有人对此问题有解决方案,请在评论中留下。
解决方案
当我请求状态时,它具有此状态的正确字段。
$ okurl https://api.twitter.com/1.1/statuses/show/1212893878430420992.json
...
"geo":null,
"coordinates":null,
"place":{"id":"fd110fb449209bc4","url":"https:\/\/api.twitter.com\/1.1\/geo\/id\/fd110fb449209bc4.json","place_type":"city","name":"Burguillos de Toledo"
https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/geo-objects#tweet-exact
阅读“使用 Twitter Place 发布推文”和“使用确切位置发布推文”之间的区别。
我怀疑您的“瞬态”字段导致 Mongo 不保存该字段。
推荐阅读
- ios - 如何防止在表格视图单元格的拖放中将数据附加到现有单元格?
- django - 如何使用带有默认参数的 update_or_create
- pandas - 将每日股票收益转换为每周股票收益
- windows - 有没有办法在 xml 文件中找到特定的字符串,然后用批处理脚本替换它下面的下一个字符串?
- java - Spring Security HttpSecurity 配置
- reactjs - 在 React Router DOM 的历史导入中未定义 createBrowserHistory
- java - Spock 和 Groovy:变量赋值导致 null
- javascript - Ionic 4 插件 - 对象不是函数
- c++ - 为什么 std::tuple 的代码无法在 MSVC 上编译
- asp.net - MissingMethodException:升级 EF6 后无效 System.Data.Entity.Infrastructure.LocalDbConnectionFactory..ctor()