首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java freebase查询

Java freebase查询
EN

Stack Overflow用户
提问于 2012-10-13 03:38:31
回答 2查看 342关注 0票数 1

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

代码语言:javascript
复制
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();
}

但是,我得到了以下错误:

代码语言:javascript
复制
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)

我以前已经这样做了(对于其他请求),并且它起作用了,那么这里的问题是什么呢?

EN

回答 2

Stack Overflow用户

发布于 2012-10-13 04:06:31

如果你成功了!

api不能正确解析emply空格,请使用以下内容:

代码语言:javascript
复制
    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(" ", ""));
票数 1
EN

Stack Overflow用户

发布于 2012-10-13 04:21:12

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

代码语言:javascript
复制
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编码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12865934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档