2

我不知道错误发生了什么,请帮助我。

  1. HttpRequest 类型的方法 getUrl() 未定义
  2. HttpTransport 类型的方法 createRequestFactory(new HttpRequestInitializer(){}) 未定义
  3. 构造函数 JsonHttpParser(JacksonFactory) 未定义

我错过了任何图书馆吗?

谢谢你

import com.google.api.client.googleapis.GoogleHeaders;
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.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.http.json.JsonHttpParser;
import com.google.api.client.json.jackson.JacksonFactory;
public class GooglePlaces {

/** Global instance of the HTTP transport. */
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

// Google API Key
private static final String API_KEY = "my-api-key";

// Google Places serach url's
private static final String PLACES_SEARCH_URL = "https://maps.googleapis.com/maps/api/place/search/json?";
private static final String PLACES_TEXT_SEARCH_URL = "https://maps.googleapis.com/maps/api/place/search/json?";
private static final String PLACES_DETAILS_URL = "https://maps.googleapis.com/maps/api/place/details/json?";

private double _latitude;
private double _longitude;
private double _radius;

/**
 * Searching places
 * @param latitude - latitude of place
 * @params longitude - longitude of place
 * @param radius - radius of searchable area
 * @param types - type of place to search
 * @return list of places
 * */
public PlacesList search(double latitude, double longitude, double radius, String types)
        throws Exception {

    this._latitude = latitude;
    this._longitude = longitude;
    this._radius = radius;

    try {

        HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
        HttpRequest request = httpRequestFactory
                .buildGetRequest(new GenericUrl(PLACES_SEARCH_URL));
        request.getUrl().put("key", API_KEY);
        request.getUrl().put("location", _latitude + "," + _longitude);
        request.getUrl().put("radius", _radius); // in meters
        request.getUrl().put("sensor", "false");
        if(types != null)
            request.getUrl().put("types", types);

        PlacesList list = request.execute().parseAs(PlacesList.class);
        // Check log cat for places response status
        Log.d("Places Status", "" + list.status);
        return list;

    } catch (HttpResponseException e) {
        Log.e("Error:", e.getMessage());
        return null;
    }

}

/**
 * Searching single place full details
 * @param refrence - reference id of place
 *                 - which you will get in search api request
 * */
public PlaceDetails getPlaceDetails(String reference) throws Exception {
    try {

        HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
        HttpRequest request = httpRequestFactory
                .buildGetRequest(new GenericUrl(PLACES_DETAILS_URL));
        request.getUrl().put("key", API_KEY);
        request.getUrl().put("reference", reference);
        request.getUrl().put("sensor", "false");

        PlaceDetails place = request.execute().parseAs(PlaceDetails.class);

        return place;

    } catch (HttpResponseException e) {
        Log.e("Error in Perform Details", e.getMessage());
        throw e;
    }
}

/**
 * Creating http request Factory
 * */
public static HttpRequestFactory createRequestFactory(
        final HttpTransport transport) {
    return transport.createRequestFactory(new HttpRequestInitializer() {
        public void initialize(HttpRequest request) {
            GoogleHeaders headers = new GoogleHeaders();
            headers.setApplicationName("AndroidHive-Places-Test");
            request.setHeaders(headers);
            JsonHttpParser parser = new JsonHttpParser(new JacksonFactory());
            request.addParser(parser);
        }
    });
}

}
4

1 回答 1

0

这是相当古老的。但是如果有人遇到类似的问题,这对我有用。

看来我有一些相互矛盾的依赖关系:

//implementation("com.google.api.client:google-api-client-json:1.2.3-alpha")
//implementation("com.google.api.client:google-api-client-util:1.2.3-alpha")
implementation("com.google.http-client:google-http-client:1.40.0")
implementation("com.google.http-client:google-http-client-jackson2:1.40.0")

当我删除前两个时,它起作用了。

在我的调查中,我看到 jar 文件google-http-client-1.40.0-sources.jar缺少方法(在 Intellij 上,我使用了命令“转到声明或使用”),而在HttpTransport.class反编译的文件External Libraries中没有它。

于 2021-09-14T13:13:23.503 回答