我正在尝试给出 here 的 Java 示例程序在 Freebase 文档中。
这是程序
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.jayway.jsonpath.JsonPath;
import java.io.FileInputStream;
import java.util.Properties;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class TopicSample {
public static Properties properties = new Properties();
public static void main(String[] args) {
try {
properties.load(new FileInputStream("freebase.properties"));
HttpTransport httpTransport = new NetHttpTransport();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
JSONParser parser = new JSONParser();
String query = "[{\"id\":null,\"name\":null,\"type\":\"/astronomy/planet\"}]";
GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/mqlread");
url.put("query", query);
url.put("key", properties.get("API_KEY"));
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse httpResponse = request.execute();
JSONObject response = (JSONObject)parser.parse(httpResponse.parseAsString());
JSONArray results = (JSONArray)response.get("result");
for (Object result : results) {
System.out.println(JsonPath.read(result,"$.name").toString());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
我还用我自己生成的 key 替换了“API_KEY”
。
当我运行这个程序时,我收到一个错误
java.io.FileNotFoundException: freebase.properties (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at code.TopicSample.main(TopicSample.java:29)
BUILD SUCCESSFUL (total time: 0 seconds)
我必须创建“freebase.properties”
文件吗?该文件的内容是什么?我浏览了几乎完整的 Freebase API 文档,但找不到有关此文件的任何线索。这个文件可以下载吗?我希望获得有关此文件的信息的链接。
最佳答案
是的,从外观上看。您会注意到您调用了 properties.get("API_KEY")
- 所以大概您需要将 API_KEY 放入属性文件中。
因此,一个名为 freebase.properties 的文件包含:
API_KEY = <your actual API key>
关于java - Freebase API 文档中 Java 示例程序的文件未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29200769/