I cant understand this API. I have downloaded (maven) google-api-services-freebase and to test I have tried to do this code:
JsonHttpRequestInitializer initializer =
new GoogleKeyInitializer("MY KEY");
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
Freebase freebase =
Freebase.builder(httpTransport,
jsonFactory).setJsonHttpRequestInitializer(initializer).build();
Now i want to do this:
https://www.googleapis.com/freebase/v1/search?query=cardiff&lang=en&type=university
And next, I want to parse the json output.
Please, where is the method to this in freebase object?. I see freebase.mqlread and freebase.text but i cant find any method similar for API SEARCH (http://wiki.freebase.com/wiki/ApiSearch).
Unfortunately, the Java API Client doesn't support Freebase Search yet. Here's how to do a search using Apache HttpClient instead.
HttpClient httpclient = new DefaultHttpClient();
JSONParser parser = new JSONParser();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("query", "Blue Bottle"));
params.add(new BasicNameValuePair("key", API_KEY));
String serviceURL = "https://www.googleapis.com/freebase/v1/search";
String url = serviceURL + "?" + URLEncodedUtils.format(params, "UTF-8");
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
JSONObject response = (JSONObject)parser.parse(EntityUtils.toString(httpResponse.getEntity()));
JSONArray results = (JSONArray)response.get("result");
for (Object result : results) {
System.out.println(JsonPath.read(result,"$.name").toString());
}
This code also uses the json-simple and json-path libraries.
Thank you.
Here is my code:
import com.google.gson.*;
import org.apache.http.*;
//...
public static void searchTest(String query, String key, String params) throws IOException
{
String query_envelope = "{\"query\":" + query + "}";
String service_url = "https://www.googleapis.com/freebase/v1/search";
String url = service_url + "?query=" + URLEncoder.encode(query, "UTF-8")
+ params
+ "&key=" + key;
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
JsonParser parser = new JsonParser();
JsonObject json_data =
(JsonObject)parser.parse(EntityUtils.toString(response.getEntity()));
JsonArray results = (JsonArray)json_data.get("result");
if(results != null)
{
for (Object planet : results)
{
System.out.println(((JsonObject)planet).get("name"));
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.