2017-03-03 5 views
1

を使用して、GoogleのナレッジグラフAPIを適用カント: https://developers.google.com/knowledge-graph/kgsearch.propertiesとは何ですか?それは、ここからコピーされる。これは、Eclipseの 上で実行私のコードであるJavaの

インストールされているすべてのライブラリと、プログラムが正常にコンパイルすることができますが、プログラムは のjavaで終了します。 io.FileNotFoundException:kgsearch.properties(そのようなファイルまたはディレクトリはありません)エラーです。私のEclipse上で次のサンプルコードをどのように動かすことができますか?

import com.google.api.client.http.GenericUrl; 
import com.google.api.client.http.HttpRequest; 
import com.google.api.client.http.HttpRequestFactory; 
import com.google.api.client.http.HttpResponse; 
import com.google.api.client.http.HttpTransport; 
import com.google.api.client.http.javanet.NetHttpTransport; 

import com.jayway.jsonpath.JsonPath; 

import org.json.simple.JSONArray; 
import org.json.simple.JSONObject; 
import org.json.simple.parser.JSONParser; 

import java.io.FileInputStream; 
import java.util.Properties; 

public class mainS { 
    public static Properties properties = new Properties(); 
    public static void main(String[] args) { 
    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("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 (Object element : elements) { 
     System.out.println(JsonPath.read(element, "$.result.name").toString()); 
     } 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
    } 
} 

答えて

0

はい、kgsearch.propertiesが存在しない可能性があります。 ファイルを作成し、そこでAPI_KEYを入力してください。あなたが作成したファイルを保存したら ファイルには、それは同様の問題についてのあとがき

読むに動作するはずです、次のコードスニペット

File file = new File("C:/your/path/here/", "kgsearch.properties"); 
properties.load(new FileInputStream(file)); 

を使用

API_KEY = <your_key_here>

のようになります。 hereおよび例here

関連する問題