Java 自由库查询

标签 java url freebase

我正在尝试使用 Java 读取 freebase 查询的结果:

URL url = null;
String inputLine;

try{
  url = new URL("https://www.googleapis.com/freebase/v1/mqlread?query={\"id\":\"/en/madonna\", \"name\": null, \"type\":\"/base/popstra/celebrity\",\"/base/popstra/celebrity/friendship\": [{\"participant\": [] }] }");

} catch (MalformedURLException e) {
  e.printStackTrace();
}
BufferedReader in;
try {
  URLConnection con = url.openConnection();
  con.setReadTimeout( 1000 ); //1 second
  in = new BufferedReader(new InputStreamReader(con.getInputStream()));
  while ((inputLine = in.readLine()) != null) {
  System.out.println(inputLine);
}
in.close();

} catch (IOException e) {
  e.printStackTrace();
}

但是,我收到以下错误:

java.io.IOException: Server returned HTTP response code: 400 for URL:     https://www.googleapis.com/freebase/v1/mqlread?query={ "id":"/en/madonna", "name": null, "type": "/base/popstra/celebrity","/base/popstra/celebrity/friendship": [{ "participant": [] }] }

at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at FreebaseCelebrities.main(FreebaseCelebrities.java:66)

我之前已经这样做过(与其他请求一起)并且它有效,那么这里有什么问题?

最佳答案

如果有的话!!!

API 未正确解析空空格,请使用此:

    url = new URL("https://www.googleapis.com/freebase/v1/mqlread?query={\"id\":\"/en/madonna\", \"name\": null, \"type\":\"/base/popstra/celebrity\",\"/base/popstra/celebrity/friendship\": [{\"participant\": [] }] }".replace(" ", ""));

关于Java 自由库查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12865934/

相关文章:

java - 如何使用 Jexcel API 在 Java 中编辑/修改现有 Excel 文件

python - 如何像 Freebase 一样存储数据?

sparql - 使用 WikiData Sparql 高效查询抽象元素

javascript - 无法使用 Jquery 从 Freebase 导出额外数据

java - Google Chart Tool Api 是否可以免费使用

java - Spring Boot - 识别资源文件夹下的资源

java - 如何强制 pig 增加 map 数量

javascript - 如何构建一个可用于多个 URL 的 Backbone 模型?

javascript - 使用 JavaScript 隐藏链接 URL

url - 是否有用于将短网址翻译回常规链接的 API?