1

我正在使用 google places API,在解析中我使用的是 JSON,但是这一行

JsonHttpParser parser = new JsonHttpParser(new JacksonFactory());
request.addParser(parser);

已弃用。有什么方法可以修复或任何替代方法吗?

这是功能:

public Placeslist search(double latitude, double longitude, double radius, String types)
        throws Exception {

    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);  
        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;
    }
}  

// 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("Google-Places-Test");
            request.setHeaders(headers);
            JsonHttpParser parser = new JsonHttpParser(new JacksonFactory());
            request.addParser(parser);
        }
    });
}
4

1 回答 1

5

在 1.11 版中似乎不推荐使用类JsonHttpParser和方法。HttpRequest有关详细信息,请参阅弃用列表

所以而不是:

JsonHttpParser parser = new JsonHttpParser(new JacksonFactory());
request.addParser(parser);

您现在将拥有:

JsonObjectParser parser = new JsonObjectParser(new JacksonFactory());
request.setParser(parser);

并将您的导入更改为:

import com.google.api.client.json.JsonObjectParser;

编辑:我已经对此进行了测试,它对我有用。

于 2012-12-01T17:21:00.253 回答