API密钥基于Android应用程序的Sha1进行限制。如果在验证密钥时出现错误,请使用以下步骤解决问题:
private static final String API_KEY = "YOUR_API_KEY";
private static final String BASE_URL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?";
public interface PlacesService {
@GET(BASE_URL)
Call getNearbyPlaces(
@Query("key") String apiKey,
@Query("location") String location,
@Query("radius") int radius,
@Query("type") String type);
}
public class PlacesApiClient {
private static final String TAG = PlacesApiClient.class.getSimpleName();
private static PlacesApiClient instance;
private Retrofit retrofit;
private PlacesApiClient() {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(loggingInterceptor);
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
}
public static PlacesApiClient getInstance() {
if (instance == null) {
instance = new PlacesApiClient();
}
return instance;
}
public PlacesService getPlacesService() {
return retrofit.create(PlacesService.class);
}
}