我想在我的某个Android应用中使用Google Places API。但是我收到了这个错误:
com.google.android.gms.tasks.RuntimeExecutionException: com.google.android.gms.common.api.ApiException:9008: PLACES_API_INVALID_APP
到目前为止我做了什么。
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_places_api_key" />
文件中添加了AndroidManifest.xml
。我的代码如下所示。
EmploymentDetailsActivity.java
public class EmploymentDetailsActivity extends BaseActivity {
@BindView(R.id.aet_employment_employer)
AutoCompleteTextView employerAutoCompleteTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employment_details);
ButterKnife.bind(this);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mGeoDataClient = Places.getGeoDataClient(this);
final GooglePlacesAdapter adapter = new GooglePlacesAdapter(this);
employerAutoCompleteTextView.setAdapter(adapter);
employerAutoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final AutocompletePrediction item = adapter.getItem(position);
final String placeId = item.getPlaceId();
final CharSequence primaryText = item.getPrimaryText(null);
employerAutoCompleteTextView.setText(primaryText);
Task<PlaceBufferResponse> placeResult = mGeoDataClient.getPlaceById(placeId);
placeResult.addOnCompleteListener(mUpdatePlaceDetailsCallback);
}
});
}
}
GooglePlacesAdapter.java
public class GooglePlacesAdapter extends ArrayAdapter<AutocompletePrediction> implements Filterable {
private Context mContext;
private GeoDataClient mGeoDataClient;
private static final String TAG = "mk";
private ArrayList<AutocompletePrediction> mResultList;
public GooglePlacesAdapter(Context context) {
super(context, android.R.layout.simple_dropdown_item_1line);
this.mContext = context;
mGeoDataClient = Places.getGeoDataClient(mContext);
}
@Override
public int getCount() {
return mResultList.size();
}
@Override
public AutocompletePrediction getItem(int position) {
return mResultList.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_google_places, parent, false);
}
AutocompletePrediction autocompletePrediction = getItem(position);
TextView placeName = convertView.findViewById(R.id.tv_primary_text);
TextView placeAddress = convertView.findViewById(R.id.tv_secondary_text);
placeName.setText(autocompletePrediction.getPrimaryText(null));
placeAddress.setText(autocompletePrediction.getSecondaryText(null));
return convertView;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null) {
mResultList = getAutocomplete(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) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
}
private ArrayList<AutocompletePrediction> getAutocomplete(CharSequence constraint) {
AutocompleteFilter.Builder filterBuilder = new AutocompleteFilter.Builder();
filterBuilder.setTypeFilter(AutocompleteFilter.TYPE_FILTER_NONE).setCountry("IN");
Log.e(TAG, "constraint.toString(): " + constraint.toString());
Task<AutocompletePredictionBufferResponse> results =
mGeoDataClient.getAutocompletePredictions(constraint.toString(), null,
filterBuilder.build());
// 60s for a result from the API.
try {
Tasks.await(results, 60, TimeUnit.SECONDS);
} catch (ExecutionException | InterruptedException | TimeoutException e) {
e.printStackTrace();
}
try {
AutocompletePredictionBufferResponse autocompletePredictions = results.getResult();
// Freeze the results immutable representation that can be stored safely.
return DataBufferUtils.freezeAndClose(autocompletePredictions);
} catch (RuntimeExecutionException e) {
// If the query did not complete successfully return null
Toast.makeText(getContext(), "Error contacting API: " + e.toString(), Toast.LENGTH_SHORT).show();
Log.e(TAG, "Error getting autocomplete prediction API call", e);
return null;
}
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.xxxxxxxx.xxxxx">
<application
android:name=".ConsumerApplication"
android:allowBackup="true"
android:icon="@mipmap/app_icon"
android:label="@string/app_name"
android:roundIcon="@mipmap/app_icon"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_places_api_key" />
</application>
</manifest>
的strings.xml
<resources>
<string name="google_places_api_key">AIzxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</string>
</resources>
build.gradle(app)
dependencies {
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.google.android.gms:play-services-maps:15.0.1'
ext {
playServicesVersion = '15.0.1'
}
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.razorpay:checkout:1.4.7'
compile "com.google.android.gms:play-services-vision:${playServicesVersion}"
compile "com.google.android.gms:play-services-location:${playServicesVersion}"
//compile "com.google.android.gms:play-services:${playServicesVersion}"
compile "com.google.android.gms:play-services-places:${playServicesVersion}"
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:cardview-v7:26.1.0'
compile 'com.android.support:recyclerview-v7:26.1.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.volley:volley:1.0.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'org.apache.commons:commons-lang3:3.4'
compile 'commons-io:commons-io:2.4'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
compile 'com.google.dagger:dagger:2.10'
compile 'com.jakewharton:butterknife:8.5.1'
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
compile 'com.squareup.okio:okio:1.13.0'
compile 'com.google.code.gson:gson:2.7'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.android.support:multidex:1.0.1'
compile 'org.apache.commons:commons-lang3:3.4'
compile 'com.android.support:design:26.1.0'
compile 'com.github.arthurghazaryan:floatingactionmenu:1.0.0'
compile 'com.quiklo.jsonapi.customer:cust-app:1.0'
compile 'com.quiklo.jsonapi.shared:basic:1.0'
compile 'com.olmec.smartloan.shared:shared-api:1.0'
compile 'com.jaredrummler:material-spinner:1.1.0'
compile 'com.f2prateek.dart:dart:2.0.2'
compile 'com.f2prateek.dart:henson:2.0.2'
// compile 'uk.co.markormesher:android-fab:2.2.2'
testCompile 'junit:junit:4.12'
annotationProcessor 'com.f2prateek.dart:dart-processor:2.0.2'
annotationProcessor 'com.f2prateek.dart:henson-processor:2.0.2'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.10'
annotationProcessor 'com.google.guava:guava:19.0'
}
apply plugin: 'com.google.gms.google-services'
如果需要任何详细信息,请告诉我。提前谢谢。
答案 0 :(得分:3)
我得到了解决方案。导致问题的是包名称。
在 build.gradle(模块:应用)我有 applicationIdSuffix ,当您切换到模拟时,它会更改包名称版本
由于包名称将更改, CORRECT SHA-1指纹也将无效。因此,我需要附加 .mock 并将其放入 Google API控制台,然后开始工作。
productFlavors {
mock {
versionNameSuffix ":test"
applicationIdSuffix = ".mock"
buildConfigField "String", "PING_URL", '"https://demoapps.xxxxxxxxx.com/"'
signingConfig signingConfigs.ReleaseBuild
}
prod {
buildConfigField "String", "PING_URL", '"https://apps.xxxxxxxxx.com/"'
}
}
答案 1 :(得分:0)
您很可能错过了其中一个