user3487667
user3487667

Reputation: 529

Not regonize the createRequestFactory() method for the type HttpTransport

I start working with FreeBase api so I only copy the following code from this page but I got the following error:

The method createRequestFactory() is undefined for the type HttpTransport

from this line of the sample code:
(HttpRequestFactory requestFactory = httpTransport.createRequestFactory()).

I try to find the problem but I could not.

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 java.io.FileInputStream;
import java.util.Properties;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class TopicSample {
 public static Properties properties = new Properties();
 public static void main(String[] args) {
  try {
    properties.load(new FileInputStream("freebase.properties"));
    HttpTransport httpTransport = new NetHttpTransport();
    HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
    JSONParser parser = new JSONParser();
    String topicId = "/en/bob_dylan";
    GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/topic" + topicId);
    url.put("key", properties.get("API_KEY"));
    HttpRequest request = requestFactory.buildGetRequest(url);
    HttpResponse httpResponse = request.execute();
    JSONObject topic = (JSONObject)parser.parse(httpResponse.parseAsString());
      System.out.println(JsonPath.read(topic,"$.property['/type/object/name'].values[0].value").toString());
} catch (Exception ex) {
  ex.printStackTrace();}}}

I do not know what should I do to solve this problem.

Upvotes: 1

Views: 602

Answers (2)

user3487667
user3487667

Reputation: 529

The following image shows all the needed library for solving the problem or I can say run the code completely correct. enter image description here

Upvotes: 1

Eli Omernick
Eli Omernick

Reputation: 196

What version of the library are you using? I've had the same issue; got it working with 1.4.1-beta.

Maven link: http://mvnrepository.com/artifact/com.google.api.client/google-api-client/1.4.1-beta

Upvotes: 0

Related Questions