【发布时间】:2020-12-21 05:33:38
【问题描述】:
我写了一个TwitterAPI 来访问 twitter api。
public class TwitterAPI {
private String twitterApiKey;
private String twitterAPISecret;
final static String TWITTER_TOKEN_URL = "https://api.twitter.com/oauth2/token";
final static String TWITTER_STREAM_URL = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=";
public TwitterAPI(String twitterAPIKey, String twitterApiSecret) {
this.twitterApiKey = twitterAPIKey;
this.twitterAPISecret = twitterApiSecret;
}
public ArrayList<TwitterTweet> getTwitterTweets(String screenName) {
ArrayList<TwitterTweet> twitterTweetArrayList = null;
try {
String twitterUrlApiKey = URLEncoder.encode(twitterApiKey, "UTF-8");
String twitterUrlApiSecret = URLEncoder.encode(twitterAPISecret, "UTF-8");
String twitterKeySecret = twitterUrlApiKey + ":" + twitterUrlApiSecret;
String twitterKeyBase64 = Base64.encodeToString(twitterKeySecret.getBytes(),
Base64.NO_WRAP);
TwitterAuthToken twitterAuthToken = getTwitterAuthToken(twitterKeyBase64);
twitterTweetArrayList = getTwitterTweets(screenName, twitterAuthToken);
} catch (UnsupportedEncodingException | IllegalStateException ex) {
ex.printStackTrace();
}
return twitterTweetArrayList;
}
public ArrayList<TwitterTweet> getTwitterTweets(String screenName,
TwitterAuthToken twitterAuthToken) {
ArrayList<TwitterTweet> twitterTweetArrayList = null;
if (twitterAuthToken != null && twitterAuthToken.token_type.equals("bearer")) {
HttpGet httpGet = new HttpGet(TWITTER_STREAM_URL + screenName);
httpGet.setHeader("Authorization", "Bearer "
+twitterAuthToken.access_token);
httpGet.setHeader("Content-Type", "application/json");
HttpUtil httpUtil = new HttpUtil();
String twitterTweets = httpUtil.getHttpResponse(httpGet);
twitterTweetArrayList = convertJsonToTwitterTweet(twitterTweets);
}
return twitterTweetArrayList;
}
public TwitterAuthToken getTwitterAuthToken(String twitterKeyBase64)
throws UnsupportedEncodingException {
HttpPost httpPost = new HttpPost(TWITTER_TOKEN_URL);
httpPost.setHeader("Authorization", "Basic " + twitterKeyBase64);
httpPost.setHeader("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
httpPost.setEntity(new StringEntity("grant_type=client_credentials"));
HttpUtil httpUtil = new HttpUtil();
String twitterJsonResponse = httpUtil.getHttpResponse(httpPost);
return convertJsonToTwitterAuthToken(twitterJsonResponse);
}
private TwitterAuthToken convertJsonToTwitterAuthToken(String jsonAuth) {
TwitterAuthToken twitterAuthToken = null;
if (jsonAuth != null && jsonAuth.length() > 0) {
try {
Gson gson = new Gson();
twitterAuthToken = gson.fromJson(jsonAuth, TwitterAuthToken.class);
} catch (IllegalStateException ex) {
ex.printStackTrace();
}
}
return twitterAuthToken;
}
private ArrayList<TwitterTweet> convertJsonToTwitterTweet(String twitterTweets) {
ArrayList<TwitterTweet> twitterTweetArrayList = null;
if (twitterTweets != null && twitterTweets.length() > 0) {
try {
Gson gson = new Gson();
twitterTweetArrayList = gson.fromJson(twitterTweets,
new TypeToken<ArrayList<TwitterTweet>>() {
}.getType());
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
return twitterTweetArrayList;
}
private static class TwitterAuthToken {
String token_type;
String access_token;
}
}
然后我在async中使用如下
public class TwitterAsyncTask extends AsyncTask<Object, Void, ArrayList<TwitterTweet>> {
ListActivity callerActivity;
@Override
protected ArrayList<TwitterTweet> doInBackground(Object... params) {
ArrayList<TwitterTweet> twitterTweets = null;
callerActivity = (ListActivity) params[1];
if (params.length > 0) {
TwitterAPI twitterAPI = new TwitterAPI(TWITTER_API_KEY, TWITTER_API_SECRET);
twitterTweets = twitterAPI.getTwitterTweets(params[0].toString());
}
return twitterTweets;
}
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(ArrayList<TwitterTweet> twitterTweets) {
ArrayAdapter<TwitterTweet> adapter = new ArrayAdapter<>(callerActivity,
R.layout.activity_twitter_view, R.id.listTextView, twitterTweets);
callerActivity.setListAdapter(adapter);
ListView lv = callerActivity.getListView();
lv.setDividerHeight(0);
//lv.setDivider(this.getResources().getDrawable(android.R.color.transparent));
lv.setBackgroundColor(callerActivity.getResources().getColor(R.color.color_white));
}
}
【问题讨论】:
标签: android twitter android-asynctask