在android上实现谷歌知识图 [英] implementing google knowledge graph on android
问题描述
Google知识图谱搜索API
zip here 并将其导入到我的项目中,但对于Android我建议按照此处所述使用 Google Play服务API
developers.google.com/api-client-library/java/google-api-java-client / setup#android
(对于这个链接抱歉,由于数据较低,我不允许发布超过2个链接,因此我即兴发布),然后提供了一段可用于查询 API
下面是代码 private void doSearch(){
System.out.println(search started);
属性properties = new Properties();
尝试
{
properties.load(new FileInputStream(kgsearch.properties));
HttpTransport httpTransport = new NetHttpTransport();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
JSONParser parser = new JSONParser();
GenericUrl url = new GenericUrl(https://kgsearch.googleapis.com/v1/entities:search);
url.put(query,Taylor Swift);
url.put(limit,10);
url.put(indent,true);
url.put(key,properties.get(ConstantsUtil.GOOGLE_API_KEY));
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse httpResponse = request.execute();
JSONObject响应=(JSONObject)parser.parse(httpResponse.parseAsString());
JSONArray元素=(JSONArray)response.get(itemListElement);
for(int i = 0; i< elements.length(); i ++)
{
System.out.println(JsonPath.read(elements.getJSONObject(i ),$ .result.name)。toString());
catch(Exception e)
{
e.printStackTrace();
}
System.out.println(search ended);
}
但是这段代码并不完整,当我运行它时,它抱怨缺少文件 kgsearch
java.io.FileNotFoundException:/kgsearch.properties:打开失败:ENOENT(没有这样的文件或目录)
但我的问题是我如何解决它?有没有人曾经使用 Google知识图谱API
和我可以使用的更多工作示例代码?如果有任何请帮助。
Properties
对象,我会看看需要为剩下的代码设置什么设置。 您应该尝试对您的GOOGLE_API_KEY字符串进行硬编码,看看是否需要这些。
这个示例代码很可能试图显示一个正确的方式处理API_KEYs作为条目存储在属性文件中。
I have been struggling with Google Knowledge Graph Search API
for Android for 2 days now, according to Google here all I have to do is download the Google Knowledge Graph Search API
zip here and import it into my project but for Android I am advised to use the Google Play Services API
as explained here developers.google.com/api-client-library/java/google-api-java-client/setup#android
(sorry about this link, because of low stats I was not allowed to post more than 2 links so I improvised) , then there is a piece of java code supplied which can be used to query the API
below is the code
private void doSearch() {
System.out.println("search started");
Properties properties = new Properties();
try
{
properties.load(new FileInputStream("kgsearch.properties"));
HttpTransport httpTransport = new NetHttpTransport();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
JSONParser parser = new JSONParser();
GenericUrl url = new GenericUrl("https://kgsearch.googleapis.com/v1/entities:search");
url.put("query", "Taylor Swift");
url.put("limit", "10");
url.put("indent", "true");
url.put("key", properties.get(ConstantsUtil.GOOGLE_API_KEY));
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse httpResponse = request.execute();
JSONObject response = (JSONObject) parser.parse(httpResponse.parseAsString());
JSONArray elements = (JSONArray) response.get("itemListElement");
for(int i = 0; i < elements.length(); i++)
{
System.out.println(JsonPath.read(elements.getJSONObject(i), "$.result.name").toString());
}
}catch(Exception e)
{
e.printStackTrace();
}
System.out.println("search ended");
}
but this code is not complete, when I run it, it complains about a missing file kgsearch
java.io.FileNotFoundException: /kgsearch.properties: open failed: ENOENT (No such file or directory)
but my questions is how do I fix it? is there anybody who has ever used the Google Knowledge Graph API
with a more working sample code I could use? if there is any please help.
You seem to be hung up on the Properties
object. If you look at your example code all it appears to do is just load in the GOOGLE_API_KEY. If the Properties
object was referenced elsewhere, I would look to see what needs to be setup for the rest of the code to work.
You should just try to hardcode your GOOGLE_API_KEY string there and see if that is all that is required.
Most likely the example code is trying to show one correct way of handling API_KEYs stored as an entry in a Properties file.
这篇关于在android上实现谷歌知识图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!