gpt4 book ai didi

java - 使用 Twitter API 从推文获取坐标

转载 作者:行者123 更新时间:2023-11-30 05:21:00 25 4
gpt4 key购买 nike

我正在尝试使用 Twitter API 和 Java 正确地从推文中获取信息。我试图从 this tweet 获取准确的坐标启用了地理标签。

从现在开始,我创建了相应的 Java 类 documentation规定,但也许我不太了解地理对象,因为坐标、边界框或地点都没有出现。我的猜测是我没有正确声明这些类。

对于 Tweet 类:

// 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 信息时,我得到以下数据,并且如您所见,字段坐标地点没有出现。如果有用的话,我正在使用 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
}
}

编辑:如答案中所述,问题在于实现,因为当我从 Place 对象中删除 transient 修饰符时该程序不起作用。如果有人解决了这个问题,请将其留在评论中。

最佳答案

当我请求状态时,它具有该状态的正确字段。

$ 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 位置发推文”和“通过确切位置发推文”之间的区别。

我怀疑你的“ transient ”字段导致 Mongo 不保存该字段。

关于java - 使用 Twitter API 从推文获取坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59572145/

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