简体   繁体   English

Freebase API文档中Java中的示例程序的文件未找到错误

[英]File not found error for sample program in Java on Freebase API documentation

I am trying the sample program in Java given here in the Freebase documentation. 我正在尝试Freebase文档中此处提供的Java示例程序。

Here is the program 这是程序

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

I have also replaced "API_KEY" with my own generated key. 我还用自己生成的密钥替换了"API_KEY"

When I run this program I am getting an error 当我运行该程序时,出现错误

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)

Do I have to create "freebase.properties" file? 我必须创建"freebase.properties"文件吗? What would be the content of that file? 该文件的内容是什么? I have skimmed through almost the complete documentation of the Freebase API but I could not find any clue about this file. 我已经浏览了Freebase API的几乎完整文档,但是找不到有关此文件的任何线索。 Is this file available for download? 该文件可以下载吗? I would appreciate a link to the information about this file. 我希望获得指向该文件信息的链接。

Yes, by the looks of it. 是的,从外观上看。 You'll notice you have a call to properties.get("API_KEY") - so presumably you need to put your API_KEY in the properties file. 您会注意到您已经调用了properties.get("API_KEY") -因此,大概需要将API_KEY放入属性文件中。

So, a file called freebase.properties containing: 因此,一个名为freebase.properties的文件包含:

API_KEY = <your actual API key> API_KEY = <your actual API key>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM