android:如何从 twitter 获取趋势?

Posted

技术标签:

【中文标题】android:如何从 twitter 获取趋势?【英文标题】:android: how to get trends from twitter? 【发布时间】:2016-02-19 11:47:21 【问题描述】:

我想从 Twitter 获取趋势。任何人都可以帮我解决这个问题。我已经使用login button 登录并获得了活动会话,现在问题是如何获取趋势哈希标签。

【问题讨论】:

【参考方案1】:

https://dev.twitter.com/rest/reference/get/trends/place

示例请求:

https://api.twitter.com/1.1/trends/place.json?id=1

其中 id 是 WOEID - Yahoo!地点 ID

对于全局:1

对于印度:23424975

访问 twitterapi 需要一个 Authorization 作为标题。

public class ConstantsUtils 

    public static final String URL_ROOT_TWITTER_API = "https://api.twitter.com";
    public static final String URL_SEARCH = URL_ROOT_TWITTER_API + "/1.1/search/tweets.json?q=";
    public static final String URL_AUTHENTICATION = URL_ROOT_TWITTER_API + "/oauth2/token";

    public static final String URL_INDIA_TRENDING ="https://api.twitter.com/1.1/trends/place.json?id=23424977";


    public static final String CONSUMER_KEY = "your key";
    public static final String CONSUMER_SECRET = "your key";



获取授权令牌

public static final String TAG = "TwitterUtils";

    public static String appAuthentication() 

        HttpURLConnection httpConnection = null;
        OutputStream outputStream = null;
        BufferedReader bufferedReader = null;
        StringBuilder response = null;

        try 
            URL url = new URL(ConstantsUtils.URL_AUTHENTICATION);
            httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setRequestMethod("POST");
            httpConnection.setDoOutput(true);
            httpConnection.setDoInput(true);

            String accessCredential = ConstantsUtils.CONSUMER_KEY + ":"
                    + ConstantsUtils.CONSUMER_SECRET;
            String authorization = "Basic "
                    + Base64.encodeToString(accessCredential.getBytes(),
                            Base64.NO_WRAP);
            String param = "grant_type=client_credentials";

            httpConnection.addRequestProperty("Authorization", authorization);
            httpConnection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded;charset=UTF-8");
            httpConnection.connect();

            outputStream = httpConnection.getOutputStream();
            outputStream.write(param.getBytes());
            outputStream.flush();
            outputStream.close();
            // int statusCode = httpConnection.getResponseCode();
            // String reason =httpConnection.getResponseMessage();

            bufferedReader = new BufferedReader(new InputStreamReader(
                    httpConnection.getInputStream()));
            String line;
            response = new StringBuilder();

            while ((line = bufferedReader.readLine()) != null) 
                response.append(line);
            

            Log.d(TAG,
                    "POST response code: "
                            + String.valueOf(httpConnection.getResponseCode()));
            Log.d(TAG, "JSON response: " + response.toString());

         catch (Exception e) 
            Log.e(TAG, "POST error: " + Log.getStackTraceString(e));

         finally 
            if (httpConnection != null) 
                httpConnection.disconnect();
            
        
        return response.toString();
    

为了获取趋势。

    public static String getTimelineForSearchTerm(String twitt_url,
            Context context) 
        HttpURLConnection httpConnection = null;
        BufferedReader bufferedReader = null;
        StringBuilder response = new StringBuilder();

try 
                URL url = new URL(ConstantsUtils.URL_INDIA_TRENDING);
                httpConnection = (HttpURLConnection) url.openConnection();
                httpConnection.setRequestMethod("GET");

                String jsonString = appAuthentication();
                JSONObject jsonObjectDocument = new JSONObject(jsonString);
                String token = jsonObjectDocument.getString("token_type") + " "
                        + jsonObjectDocument.getString("access_token");
                httpConnection.setRequestProperty("Authorization", token);

                httpConnection.setRequestProperty("Authorization", token);
                httpConnection.setRequestProperty("Content-Type",
                        "application/json");
                httpConnection.connect();

                bufferedReader = new BufferedReader(new InputStreamReader(
                        httpConnection.getInputStream()));

                String line;
                while ((line = bufferedReader.readLine()) != null) 
                    response.append(line);
                

                Log.d(TAG,
                        "GET response code: "
                                + String.valueOf(httpConnection
                                        .getResponseCode()));
                Log.d(TAG, "JSON response: " + response.toString());

             catch (Exception e) 
                Log.e(TAG, "GET error: " + Log.getStackTraceString(e));

             finally 
                if (httpConnection != null) 
                    httpConnection.disconnect();

                
            
        
        return response.toString();
    

为了获取 yahoo WOEID,只需传递您所在位置的纬度和经度。

http://where.yahooapis.com/v1/places.q('"
                        + latitude
                        + ","
                        + longitude
                        + "')?appid=yourappid&format=json"

请阅读有关获取授权请求和获取授权标头的信息

https://dev.twitter.com/oauth

https://dev.twitter.com/oauth/application-only

https://dev.twitter.com/oauth/overview/authorizing-requests

【讨论】:

我知道这是 URL 但如何调用它?点击它会给我这个错误 "errors": [ "code": 215, "message": "Bad Authentication data." ] 你能解释一下我如何附加访问令牌或其他东西,因为我想在应用程序中调用它我有用户 ID 和访问令牌..所以请告诉我如何准确地调用它跨度> 这正是我所需要的 :) 非常感谢你

以上是关于android:如何从 twitter 获取趋势?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用适用于 Android 的 Fabric Sdk 从 Twitter 注销

卷曲-获取twitter趋势

这是将 Twitter 集成到 android 中的正确方法吗?谁能告诉我如何获取个人资料图片网址?

如何在按钮点击 android - twitter4j 上获取 twitter 热门话题?

如何通过 twitter 的 Fabric API (Android) 获取用户数据

快速解析 twitter 趋势 api 结果