0

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.

Chrome Lanta
  • 67
  • 1
  • 11
  • This is a basic Java programming question. I respectfully suggest that a bit more discussion about what your goals are here would lead you to a more useful answer. I mean you no offence when I say "Is there any working sample code" is more a question for Google than Stack. – Raydot Jan 02 '16 at 19:41
  • @DaveKaye I was asking someone who has used it before and how they use it, I know it is a basic java question, I can easily create the file but I do not know when to create it and if I have to create it everytime I want to perform a search and most importantly what the file houses, you could be more helpful and help a brother out, I have given links showing how much I have studied this topic trying to understand it, its not like am asking without doing my research – Chrome Lanta Jan 02 '16 at 19:57
  • @ChromeLanta - look at the example code, where does it use the Properties object? How does the example code use it? In the first link example it appears to just load in the API_KEY, at least try to hardcode the API key string as a replacement and see what happens. – Morrison Chang Jan 02 '16 at 20:11
  • @MorrisonChang I was just from observing that myself, but did not take it seriously but now that you have mentioned it let me try it. – Chrome Lanta Jan 02 '16 at 20:17
  • @MorrisonChang thanks it worked, you should have posted it as an answer, infact you should – Chrome Lanta Jan 02 '16 at 20:31
  • @ChromeLanta you should delete your earlier duplicate question if this satisfies. – Morrison Chang Jan 02 '16 at 20:46
  • @MorrisonChang thanks again – Chrome Lanta Jan 02 '16 at 22:26

1 Answers1

0

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.

Morrison Chang
  • 11,691
  • 3
  • 41
  • 77