android: 如何从推特上获取趋势?
android: how to get trends from twitter?
我想从 Twitter 获取趋势。任何人都可以帮助我 please.I 已经使用 login button
完成登录并获得活动会话现在问题是如何获得趋势哈希标签。
https://dev.twitter.com/rest/reference/get/trends/place
请求示例:
https://api.twitter.com/1.1/trends/place.json?id=1
其中 ID 是 WOEID - Yahoo! Where On Earth 地点的 ID
全球:1
印度:23424975
访问 twitterapi 需要 授权 作为 header。
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();
}
要获取雅虎 WOEID,只需传递您所在位置的经纬度即可。
http://where.yahooapis.com/v1/places.q('"
+ latitude
+ ","
+ longitude
+ "')?appid=yourappid&format=json"
请阅读有关获取授权请求和获取授权的信息header
我想从 Twitter 获取趋势。任何人都可以帮助我 please.I 已经使用 login button
完成登录并获得活动会话现在问题是如何获得趋势哈希标签。
https://dev.twitter.com/rest/reference/get/trends/place
请求示例:
https://api.twitter.com/1.1/trends/place.json?id=1
其中 ID 是 WOEID - Yahoo! Where On Earth 地点的 ID
全球:1
印度:23424975
访问 twitterapi 需要 授权 作为 header。
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();
}
要获取雅虎 WOEID,只需传递您所在位置的经纬度即可。
http://where.yahooapis.com/v1/places.q('"
+ latitude
+ ","
+ longitude
+ "')?appid=yourappid&format=json"
请阅读有关获取授权请求和获取授权的信息header