Google会解决API解析问题

时间:2012-11-28 01:34:16

标签: android json google-places-api

我正在使用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);
        }
    });
}

1 个答案:

答案 0 :(得分:5)

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

所以而不是:

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;
编辑:我测试了这个,它对我有用。