我正在调用Google的反向地理编码API,以提供纬度,经度和Google API密钥(此密钥在控制台上尚无限制),我得到的响应代码为200,但有时却得到空数据,这是问题详细:
API调用:
public static void callGeocodeAPI() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://maps.googleapis.com/maps/api/")
.addConverterFactory(GsonConverterFactory.create(getGsonInstance()))
.build();
APIservice requestInterface = retrofit.create(APIservice.class);
Call<GeocodeResponse> call = requestInterface.getReverseGeocode(latitude + "," + longitude, GOOGLE_PLACE_API_KEY);
APIHelper.enqueueWithRetry(call, new Callback<GeocodeResponse>() {
@Override
public void onResponse(Call<GeocodeResponse> call, Response<GeocodeResponse> response) {
if (response != null && response.isSuccessful()) {
GeocodeResponse jsonResponse = response.body();
if (jsonResponse != null) {
ArrayList<GeocodeResult> geocodeResponses = jsonResponse.getGeocode_results_list();
if (jsonResponse.getGeocode_results_list().size() > 0) {
Log.d(TAG, "place_id: " + geocodeResponses.get(0).getPlace_id());
Log.d(TAG, "formatted_address: " + geocodeResponses.get(0).getFormatted_address());
}
}
} else {
}
}
@Override
public void onFailure(Call<GeocodeResponse> call, Throwable t) {
Log.d(TAG, "Failure");
}
});
call.clone();
}
在Interface类中:
@GET("geocode/json")
Call<GeocodeResponse> getReverseGeocode(@Query("latlng") String latlng,
@Query("key") String API_KEY);
REST响应容器:
public class GeocodeResponse {
@SerializedName("results")
@Expose
private ArrayList<GeocodeResult> geocode_results_list;
public ArrayList<GeocodeResult> getGeocode_results_list() {
return geocode_results_list;
}
public void setGeocode_results_list(ArrayList<GeocodeResult> geocode_results_list) {
this.geocode_results_list = geocode_results_list;
}
}
GeocodeResult类:
public class GeocodeResult {
private ArrayList<GeocodeAddress> address_components;
private String formatted_address;
private GeocodeGeometry geometry;
private String place_id;
private ArrayList<String> types;
...
}
注意:我每天只发送少于300个请求。
你知道我是否缺少什么吗?