谁能告诉我有关 Traffic Junky API 的信息。我可以将它与 Java 一起使用吗?
最佳答案
基于 Web 的 API 不依赖于特定语言。您可以用任何语言使用它。 API 具有可供您使用的端点。有些具有 JSON、XML 和其他端点。
https://api.trafficjunky.com/api/doc/
文档中还有沙箱功能。 您可以使用它来查看查询的外观。
这暗示 api_key 参数可用于 API key : https://api.trafficjunky.com/api/campaigns/stats.json?api_key=123
也许您还可以使用 header 字段定义 api_key 参数。只需插入您的 API 详细信息并使用沙箱进行测试即可。
使用 URLConnection 类或 Apache Commons HttpComponents https://hc.apache.org/ 等库,在 Java 中进行 API 调用应该很容易。以及一些 JSON 库,如 json-simple、gson 和 Jackson。
只是一些不使用库的示例代码:
String api_key = "123";
HttpsURLConnection conn4 = (HttpsURLConnection)(new URL("https://api.trafficjunky.com/api/campaigns/stats.json?api_key="+api_key).openConnection());
conn4.setConnectTimeout(60000); // you may not need this or just a lower value
conn4.setReadTimeout(60000); // you may not need this or just a lower value
conn4.connect();
InputStream in = conn4.getInputStream();
InputStreamReader is3 = new InputStreamReader(in);
StringBuilder sb2=new StringBuilder();
BufferedReader br2 = new BufferedReader(is3);
String read2 = br2.readLine();
while(read2 != null) {
sb2.append(read2);
read2 =br2.readLine();
}
String json_string = sb2.toString();
// do something with the result in json_string, better use some JSON library
关于java - Java 中的 Traffic Junky API 用法和示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33888181/