Java OperationException[状态{statusCode=PLACES\u API\u RATE\u LIMIT\u Exception,resolution=null}]
我想显示位置自动建议,但获取速率\u限制\u超过错误,但我尚未超过限制。我还检查了我的谷歌控制台,它没有显示任何超出限制的消息 以下是我的PlaceAdapter.java代码:-Java OperationException[状态{statusCode=PLACES\u API\u RATE\u LIMIT\u Exception,resolution=null}],java,android,Java,Android,我想显示位置自动建议,但获取速率\u限制\u超过错误,但我尚未超过限制。我还检查了我的谷歌控制台,它没有显示任何超出限制的消息 以下是我的PlaceAdapter.java代码:- import android.content.Context; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.Filter; import android.widget.Filterable; im
import android.content.Context;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.Toast;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.AutocompleteFilter;
import com.google.android.gms.location.places.AutocompletePrediction;
import com.google.android.gms.location.places.AutocompletePredictionBuffer;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.model.LatLngBounds;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
public class PlaceArrayAdapter extends ArrayAdapter<PlaceArrayAdapter.PlaceAutocomplete> implements Filterable {
private static final String TAG = "PlaceArrayAdapter";
private GoogleApiClient mGoogleApiClient;
private AutocompleteFilter mPlaceFilter;
private LatLngBounds mBounds;
private ArrayList<PlaceAutocomplete> mResultList;
public PlaceArrayAdapter(Context context, int resource, LatLngBounds bounds,
AutocompleteFilter filter) {
super(context, resource);
mBounds = bounds;
mPlaceFilter = filter;
}
public void setGoogleApiClient(GoogleApiClient googleApiClient) {
if (googleApiClient == null || !googleApiClient.isConnected()) {
mGoogleApiClient = null;
} else {
mGoogleApiClient = googleApiClient;
}
}
@Override
public int getCount() {
return mResultList.size();
}
@Override
public PlaceAutocomplete getItem(int position) {
return mResultList.get(position);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null) {
mResultList = getPredictions(constraint);
if (mResultList != null) {
results.values = mResultList;
results.count = mResultList.size();
}
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
// The API returned at least one result, update the data.
notifyDataSetChanged();
} else {
// The API did not return any results, invalidate the data set.
notifyDataSetInvalidated();
}
}
};
return filter;
}
private ArrayList<PlaceAutocomplete> getPredictions(CharSequence constraint) {
if (mGoogleApiClient != null) {
Log.i(TAG, "Executing autocomplete query for: " + constraint);
PendingResult<AutocompletePredictionBuffer> results = Places.GeoDataApi
.getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
mBounds, null);
// Wait for predictions, set the timeout.
AutocompletePredictionBuffer autocompletePredictions = results
.await(60, TimeUnit.SECONDS);
final Status status = autocompletePredictions.getStatus();
if (!status.isSuccess()) {
Toast.makeText(getContext(), "Error: " + status.toString(),
Toast.LENGTH_SHORT).show();
Log.e(TAG, "Error getting place predictions: " + status
.toString());
autocompletePredictions.release();
return null;
}
Log.i(TAG, "Query completed. Received " + autocompletePredictions.getCount()
+ " predictions.");
Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount());
while (iterator.hasNext()) {
AutocompletePrediction prediction = iterator.next();
resultList.add(new PlaceAutocomplete(prediction.getPlaceId(),
prediction.getFullText(null)));
}
// Buffer release
autocompletePredictions.release();
return resultList;
}
Log.e(TAG, "Google API client is not connected.");
return null;
}
class PlaceAutocomplete {
public CharSequence placeId;
public CharSequence description;
PlaceAutocomplete(CharSequence placeId, CharSequence description) {
this.placeId = placeId;
this.description = description;
}
@Override
public String toString() {
return description.toString();
}
}
}
导入android.content.Context;
导入android.util.Log;
导入android.widget.ArrayAdapter;
导入android.widget.Filter;
导入android.widget.Filterable;
导入android.widget.Toast;
导入com.google.android.gms.common.api.GoogleAppClient;
导入com.google.android.gms.common.api.pendingreult;
导入com.google.android.gms.common.api.Status;
导入com.google.android.gms.location.places.AutocompleteFilter;
导入com.google.android.gms.location.places.AutoCompletePredition;
导入com.google.android.gms.location.places.AutocompletePredictionBuffer;
导入com.google.android.gms.location.places.places;
导入com.google.android.gms.maps.model.LatLngBounds;
导入java.util.ArrayList;
导入java.util.Iterator;
导入java.util.concurrent.TimeUnit;
公共类PlaceArrayAdapter扩展ArrayAdapter实现可过滤{
私有静态最终字符串TAG=“PlaceArrayAdapter”;
私人GoogleapClient MGoogleapClient;
专用自动完成过滤器;
私人LatLngBounds mBounds;
私有ArrayList mResultList;
public PlaceArrayAdapter(上下文、int资源、LatLngBounds边界、,
自动完成过滤器(过滤器){
超级(上下文、资源);
mBounds=边界;
mPlaceFilter=过滤器;
}
公共无效设置GoogleAppClient(GoogleAppClient GoogleAppClient){
if(googleApiClient==null | |!googleApiClient.isConnected()){
mgoogleapclient=null;
}否则{
mGoogleApiClient=googleApiClient;
}
}
@凌驾
public int getCount(){
返回mResultList.size();
}
@凌驾
公共场所自动完成getItem(内部位置){
返回mResultList.get(位置);
}
@凌驾
公共过滤器getFilter(){
过滤器过滤器=新过滤器(){
@凌驾
受保护的筛选器结果性能筛选(CharSequence约束){
FilterResults results=新的FilterResults();
if(约束!=null){
mResultList=getPredictions(约束);
if(mResultList!=null){
results.values=mResultList;
results.count=mResultList.size();
}
}
返回结果;
}
@凌驾
受保护的void publishResults(CharSequence约束、FilterResults结果){
if(results!=null&&results.count>0){
//API返回了至少一个结果,请更新数据。
notifyDataSetChanged();
}否则{
//API未返回任何结果,因此数据集无效。
notifyDataSetionValidated();
}
}
};
回流过滤器;
}
私有ArrayList getPredictions(CharSequence约束){
if(mGoogleApiClient!=null){
Log.i(标记“为“+约束”执行自动完成查询);
Pendingreult结果=Places.GeoDataApi
.getAutocompletePredictions(mgoogleAppClient,constraint.toString(),
mBounds,空);
//等待预测,设置超时。
AutocompletePredictionBuffer autocompletePredictions=结果
.等待(60,时间单位秒);
最终状态状态=自动完成预测。getStatus();
如果(!status.issucess()){
Toast.makeText(getContext(),“错误:+status.toString(),
吐司。长度(短)。show();
Log.e(标记“获取位置预测时出错:”+状态
.toString());
autocompletePredictions.release();
返回null;
}
Log.i(标记“querycompleted.Received”+autocompletedpredictions.getCount()
+"预测";;
迭代器迭代器=自动完成预测。迭代器();
ArrayList resultList=新的ArrayList(autocompletePredictions.getCount());
while(iterator.hasNext()){
AutocompletePrediction prediction=iterator.next();
resultList.add(新建PlaceAutocomplete(prediction.getPlaceId()),
getFullText(null));
}
//缓冲释放
autocompletePredictions.release();
返回结果列表;
}
e(标记“GoogleAPI客户端未连接”);
返回null;
}
类PlaceAutocomplete{
公共字符序列;
公共字符序列描述;
PlaceAutocomplete(字符序列placeId,字符序列描述){
this.placeId=placeId;
this.description=描述;
}
@凌驾
公共字符串toString(){
返回description.toString();
}
}
}
以下是我的xml文件的代码:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".ToFromO9Activity">
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:hint="Enter Place Here" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/autoCompleteTextView"
android:src="@drawable/powered_by_google_light" />
<TextView
android:id="@+id/select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/autoCompleteTextView"
android:layout_marginTop="20dp"
android:text="Selected Place:"
android:textStyle="bold" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/select"
android:layout_marginTop="20dp" />
</RelativeLayout>
以下是我的ToFromO9Activity.java类代码:-
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.PlaceBuffer;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
public class ToFromO9Activity extends AppCompatActivity implements
GoogleApiClient.OnConnectionFailedListener,
GoogleApiClient.ConnectionCallbacks {
private static final String TAG = "MainActivity";
private static final int GOOGLE_API_CLIENT_ID = 0;
private AutoCompleteTextView mAutocompleteTextView;
private TextView mNameView;
private GoogleApiClient mGoogleApiClient;
private PlaceArrayAdapter mPlaceArrayAdapter;
private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
new LatLng(37.398160, -122.180831), new LatLng(37.430610, -121.972090));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_from_o9);
mAutocompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
mAutocompleteTextView.setThreshold(3);
mNameView = (TextView) findViewById(R.id.name);
mGoogleApiClient = new GoogleApiClient.Builder(ToFromO9Activity.this)
.addApi(Places.GEO_DATA_API)
.enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
.addConnectionCallbacks(this)
.build();
mAutocompleteTextView.setOnItemClickListener(mAutocompleteClickListener);
mPlaceArrayAdapter = new PlaceArrayAdapter(this, android.R.layout.simple_list_item_1,
BOUNDS_MOUNTAIN_VIEW, null);
mAutocompleteTextView.setAdapter(mPlaceArrayAdapter);
}
private AdapterView.OnItemClickListener mAutocompleteClickListener
= new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final PlaceArrayAdapter.PlaceAutocomplete item = mPlaceArrayAdapter.getItem(position);
final String placeId = String.valueOf(item.placeId);
Log.i(TAG, "Selected: " + item.description);
PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
.getPlaceById(mGoogleApiClient, placeId);
placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
Log.i(TAG, "Fetching details for ID: " + item.placeId);
}
};
private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback
= new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
if (!places.getStatus().isSuccess()) {
Log.e(TAG, "Place query did not complete. Error: " +
places.getStatus().toString());
return;
}
// Selecting the first object buffer.
final Place place = places.get(0);
CharSequence attributions = places.getAttributions();
mNameView.setText(Html.fromHtml(place.getAddress() + ""));
}
};
@Override
public void onConnected(@Nullable Bundle bundle) {
mPlaceArrayAdapter.setGoogleApiClient(mGoogleApiClient);
Log.i(TAG, "Google Places API connected.");
}
@Override
public void onConnectionSuspended(int i) {
mPlaceArrayAdapter.setGoogleApiClient(null);
Log.e(TAG, "Google Places API connection suspended.");
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e(TAG, "Google Places API connection failed with error code: "
+ connectionResult.getErrorCode());
Toast.makeText(this,
"Google Places API connection failed with error code:" +
connectionResult.getErrorCode(),
Toast.LENGTH_LONG).show();
}
}
导入android.os.Bundle;
导入android.support.annotation.NonNull;
导入android.support.annotation.Nullable;
导入android.support.v7.app.AppActivity;
导入android.text.Html;
导入android.util.Log;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.AutoCompleteTextView;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.google.android.gms.common.ConnectionResult;
导入com.google.android.gms.common.api.GoogleAppClient;
导入com.go