看看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")));
}
}
检索的主题是一个稍微不同的方式来完成