2014-03-25 55 views
1

我在我的應用程序中實現谷歌的地方。但是當我創建Google Place Activity 6錯誤時。我爲圖書館崛起,在互聯網上有很多混亂。怎麼解決?我正在使用最新的google api。錯誤導入Android應用程序與谷歌的地方

The import com.google.api.client.googleapis.GoogleHeaders cannot be resolved 
The import com.google.api.client.http.json.JsonHttpParser cannot be resolved 

這是活動:

package com.example.app; 

import org.apache.http.client.HttpResponseException; 

import android.util.Log; 

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; 

@SuppressWarnings("deprecation") 
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 = "MYKEY"; 

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

} 

回答

-1

我也在這裏運行同樣的問題。到目前爲止,我已經解決了JSON依賴添加以下庫:

<dependency> 
    <groupId>com.google.http-client</groupId> 
    <artifactId>google-http-client-jackson</artifactId> 
    <version>1.15.0-rc</version> 
</dependency> 
and replacing: 
JsonHttpParser parser = new JsonHttpParser(new JacksonFactory()); 
request.addParser(parser); 
with: 
JsonObjectParser parser = new JsonObjectParser(new JacksonFactory()); 
request.setParser(parser); 

我會讓你知道如何儘快解決我要自己解決GoogleHeaders依賴。

編輯:它看起來像他們在版本1.14(看到這個問題)刪除這個類,以支持HttpHeader。用HttpHeader更改GoogleHeaders並用setUserAgent代替setApplicationName,如下所示:

HttpHeaders headers = new HttpHeaders(); 
headers.setUserAgent("AndroidHive-Places-Test"); 
相關問題