我正在尝试使用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无法正确解析emply空间,请使用以下命令:
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(" ", ""));