【问题标题】:File not found error for sample program in Java on Freebase API documentation在 Freebase API 文档上,Java 中的示例程序找不到文件错误
【发布时间】:2015-03-22 22:45:28
【问题描述】:

我正在尝试 Freebase 文档中给定 here 的 Java 示例程序。

这是程序

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();
    }
  }
}

我还用我自己生成的密钥替换了"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 文档,但我找不到关于这个文件的任何线索。这个文件可以下载吗?我将不胜感激有关此文件的信息的链接。

【问题讨论】:

    标签: java semantic-web freebase mql


    【解决方案1】:

    是的,从外观上看。您会注意到您调用了properties.get("API_KEY") - 因此您可能需要将 API_KEY 放入属性文件中。

    因此,一个名为 freebase.properties 的文件包含:

    API_KEY = &lt;your actual API key&gt;

    【讨论】:

    • 我已经提到,在我的代码中,我已将 API_KEY 替换为我生成的密钥。我不能将我的实际密钥放在公开帖子中。
    • 不行,你需要保持原样,按照上面的格式放到freebase.properties文件中。
    • 我的意思是:创建一个名为 freebase.properties 的文件并复制上面的行,但将您的真实 API 密钥改为 - 在 文件 中执行此操作 - 留下 @987654323 @ 与您的代码中的完全相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-02
    • 2020-04-18
    • 2016-10-24
    • 2018-12-01
    • 2019-01-22
    • 1970-01-01
    • 2014-08-16
    相关资源
    最近更新 更多