如何在Java中使用Twitter REST API?

时间:2015-11-04 12:01:55

标签: java rest twitter twitter4j

我正在尝试使用Twitter REST API从Twitter获取热门话题。我试过Simplest Java example retrieving user_timeline with twitter API version 1.1但我无法使它工作,因为HttpClient类在较新版本的apache中是抽象的。

所以,基本上,我不知道如何根据dev.twitter上的文档使用API​​。我唯一能做到的就是使用twitter4j发布推文,但它无法自行获取热门话题。

我也看过很多教程和操作方法,但它们似乎已经过时,没有任何效果!

1 个答案:

答案 0 :(得分:1)

检查Apache HttpClient 4.5教程here

简单Get请求似乎;

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://targethost/homepage");
CloseableHttpResponse response1 = httpclient.execute(httpGet);

在其上实施twitter api。

编辑:

HttpClient具有此执行功能;

@Override
public CloseableHttpResponse execute(
        final HttpUriRequest request) throws IOException, ClientProtocolException {
    return execute(request, (HttpContext) null);
}

接受HttpUriRequest接口(由抽象HttpRequestBase和HttpGet实现)

相关问题