看看Direct Media Tips and Tricks書的Setting up a Klout application部分。它解釋瞭如何使用dmt-klout庫來獲取您正在查找的信息。
如果要重寫庫,可以查看源代碼。 dmt-klout庫依賴於json.org類來解析JSON響應。例如:
public User(JSONObject json) {
nick = json.getString("nick");
id = new UserId(json.getString("kloutId"));
JSONObject scores = json.getJSONObject("score");
bucket = scores.getString("bucket");
score = scores.getDouble("score");
JSONObject scoreDeltas = json.getJSONObject("scoreDeltas");
dayChange = scoreDeltas.getDouble("dayChange");
weekChange = scoreDeltas.getDouble("weekChange");
monthChange = scoreDeltas.getDouble("monthChange");
}
在這種情況下json
是使用查詢用戶時返回的String
創建JSONObject
。這User
類也可用於影響查詢:
public List<Topic> getTopics(UserId id) throws IOException {
List<Topic> topics = new ArrayList<Topic>();
JSONArray array = new JSONArray(KloutRequests.sendRequest(String.format(
KloutRequests.TOPICS_FROM_KLOUT_ID, getUserId(id).getId(), apiKey)));
int n = array.length();
for (int i = 0; i < n; i++) {
topics.add(new Topic(array.getJSONObject(i)));
}
return topics;
}
的Topic
類的構造函數如下所示:
public Topic(JSONObject json) {
id = json.getLong("id");
name = json.getString("name");
displayName = json.getString("displayName");
slug = json.getString("slug");
displayType = json.getString("displayType");
imageUrl = json.getString("imageUrl");
}
public Influence(JSONObject json) {
parseInfluence(json.getJSONArray("myInfluencers"), myInfluencers);
parseInfluence(json.getJSONArray("myInfluencees"), myInfluencees);
}
private void parseInfluence(JSONArray array, List<User> list) {
int count = array.length();
for (int i = 0; i < count; i++) {
list.add(new User(
array.getJSONObject(i).getJSONObject("entity")
.getJSONObject("payload")));
}
}
檢索的主題是一個稍微不同的方式來完成