如何在Android中从用户的推特用户名中找到用户的推特ID

时间:2014-05-29 18:38:47

标签: android twitter

在我的Android应用程序中,我希望用户能够输入推特用户名,然后在输入的用户名页面上启动推特应用程序。我做了一些研究,发现从另一个应用程序打开twitter应用程序所需的链接是twitter:// user?user_id = id_num我想知道是否有办法从推特用户名获取用户的推特ID在Android中,所以我可以实现这一点。非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

您应该仔细阅读Twitter API,特别是关于user lookups的部分 编辑:链接已更新为当前的Twitter API文档,但答案仍然是针对API v1.1时的问题。

该特定请求具有可通过id或screen_name搜索的可选字段,并将返回可以为用户ID解析的匹配列表。因此,您可以通过screen_name进行搜索并阅读响应以获取ID。

您正在寻找的请求是:

GET https://api.twitter.com/1/users/lookup.json?screen_name=somename

API示例中提供的响应是(是):

[
  {
    "name": "Twitter API",
    "profile_sidebar_border_color": "87bc44",
    "profile_background_tile": false,
    "profile_sidebar_fill_color": "e0ff92",
    "location": "San Francisco, CA",
    "profile_image_url": "http://a3.twimg.com/profile_images/689684365/api_normal.png",
    "created_at": "Wed May 23 06:01:13 +0000 2007",
    "profile_link_color": "0000ff",
    "favourites_count": 2,
    "url": "http://apiwiki.twitter.com",
    "contributors_enabled": true,
    "utc_offset": -28800,
    "id": 6253282,
    "profile_use_background_image": true,
    "profile_text_color": "000000",
    "protected": false,
    "followers_count": 160752,
    "lang": "en",
    "verified": true,
    "profile_background_color": "c1dfee",
    "geo_enabled": true,
    "notifications": false,
    "description": "The Real Twitter API. I tweet about API changes, service issues and appily answer questions about Twitter and our API. Don't get an answer? It's on my website.",
    "time_zone": "Pacific Time (US & Canada)",
    "friends_count": 19,
    "statuses_count": 1858,
    "profile_background_image_url": "http://a3.twimg.com/profile_background_images/59931895/twitterapi-background-new.png",
    "status": {
      "coordinates": null,
      "favorited": false,
      "created_at": "Tue Jun 22 16:53:28 +0000 2010",
      "truncated": false,
      "text": "@Demonicpagan possible some part of your signature generation is incorrect & fails for real reasons.. follow up on the list if you suspect",
      "contributors": null,
      "id": 16783999399,
      "geo": null,
      "in_reply_to_user_id": 6339722,
      "place": null,
      "source": "<a href="http://www.tweetdeck.com" rel="nofollow">TweetDeck</a>",
      "in_reply_to_screen_name": "Demonicpagan",
      "in_reply_to_status_id": 16781827477
    },
    "screen_name": "twitterapi",
    "following": false
  },
  {
    "name": "Twitter",
    "profile_sidebar_border_color": "EEEEEE",
    "profile_background_tile": false,
    "profile_sidebar_fill_color": "F6F6F6",
    "location": "San Francisco, CA",
    "profile_image_url": "http://a1.twimg.com/profile_images/878669694/twitter_bird_normal.jpg",
    "created_at": "Tue Feb 20 14:35:54 +0000 2007",
    "profile_link_color": "038543",
    "favourites_count": 2,
    "url": "http://twitter.com",
    "contributors_enabled": true,
    "utc_offset": -28800,
    "id": 783214,
    "profile_use_background_image": true,
    "profile_text_color": "333333",
    "protected": false,
    "followers_count": 3305606,
    "lang": "en",
    "verified": true,
    "profile_background_color": "ACDED6",
    "geo_enabled": true,
    "notifications": false,
    "description": "Always wondering what's happening. ",
    "time_zone": "Pacific Time (US & Canada)",
    "friends_count": 257,
    "statuses_count": 774,
    "profile_background_image_url": "http://s.twimg.com/a/1276896641/images/themes/theme18/bg.gif",
    "status": {
      "coordinates": null,
      "favorited": false,
      "created_at": "Tue Jun 22 16:40:19 +0000 2010",
      "truncated": false,
      "text": "9 cool things to do with your Twitter account (via @pastemagazine) http://example.com",
      "contributors": [
        16739704
      ],
      "id": 16783169544,
      "geo": null,
      "in_reply_to_user_id": null,
      "place": null,
      "source": "web",
      "in_reply_to_screen_name": null,
      "in_reply_to_status_id": null
    },
    "screen_name": "twitter",
    "following": false
  }
]

因此,您可以通过解析“id”字段的JSON响应来从结果中访问id。

但请注意,返回的是JSON结果数组,而不仅仅是一个明确的答案。你需要自己解决哪个是正确的(订单不能保证,所以不要假设第一个条目最有可能)。

另外值得注意的是,尽管记录为返回'id'和整数值,但当前首选的id用法是提供'id_str'和整数的字符串表示。这是由于各种平台处理和限制整数的方式不一致。因此,即使您收到“id”,也应该使用“id_str”进行未来的互动。