Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java freebase查询_Java_Url_Freebase - Fatal编程技术网

Java freebase查询

Java freebase查询,java,url,freebase,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/friendsh

我正在尝试使用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(" ", ""));

创建URL时请尝试以下代码:

try {
  String query = "{\"id\":\"/en/madonna\", \"name\": null, \"type\":\"/base/popstra/celebrity\",\"/base/popstra/celebrity/friendship\": [{\"participant\": [] }] }";
  url = new URL("https://www.googleapis.com/freebase/v1/mqlread?query="+URLEncoder.encode(query, "UTF-8"));
} catch (UnsupportedEncodingException e) {
  e.printStackTrace();
} catch (MalformedURLException e) {
  e.printStackTrace();
}

您需要对查询字符串参数进行URL编码。

为什么不只对其进行URL编码,这样可以允许在字符串值中使用空格,而不会破坏可能正确的空格(我已经尝试过了)。您是对的,但我尝试过,使用URLEncode和Apache encodeQuery,此api的url格式不正确,在某些地方不接受空格。谢谢大家,它工作得很好。然而,当我连续进行大量查询时,我会出现一些错误,这正常吗?你是指短时间内的多次查询(谷歌规定的速率限制)还是一次包含大量信息的大型查询(最大HTTP GET大小)?短时间内的多次查询。(试图得到麦当娜的朋友,然后是麦当娜朋友的朋友,…)根据利率限制,但看起来你没有达到。这可能是你与谷歌的连接,每次都有相同的查询失败吗?我在Matlab中重新实现了该程序,结果完全相同。我的第一个查询是毫无问题地进行的,然后出现了一些问题,结果失败了(但我手动尝试了这些请求,它们可以正常工作)。每次我尝试时,程序都会以不同的有效查询失败。。。