这是我第一次尝试创建 android 应用程序。在尝试使用 Google 提供的 Awareness API 时,我在 logcat 中收到 SecurityException,并且出现错误:
“包的 API 密钥无效 = com.example.android.project。收到的状态代码 = 12”。
意思是,mGoogleApiClient.connect() 每次都会导致应用程序崩溃。有没有办法知道状态码是什么意思?
[BaseServerTask]Server task (PingTask) got error statusCode=403.
com.android.volley.AuthFailureError
at com.android.volley.toolbox.BasicNetwork.performRequest(:com.google.android.gms:32)
at lbf.performRequest(:com.google.android.gms:3)
at com.android.volley.NetworkDispatcher.run(:com.google.android.gms:11)
07-01 15:54:24.303 4839-19074/? E/ctxmgr: [ContextManager3PCredentialsVerifier]Failed ping response: network status=12
07-01 15:54:24.304 4839-19081/? E/AbstractServiceBroker: Getting service failed
java.lang.SecurityException: Invalid API Key for package = com.example.android.project. Status code received = 12
at cpm.a(:com.google.android.gms:19)
at cpp.a(:com.google.android.gms:46)
at ktv.a(:com.google.android.gms:41)
at kwh.onTransact(:com.google.android.gms:8)
at android.os.Binder.transact(Binder.java:507)
at bxh.onTransact(:com.google.android.gms:2)
at android.os.Binder.execTransact(Binder.java:573)
07-01 15:54:24.520 19508-19508/com.example.android.project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.project, PID: 19508
java.lang.SecurityException: Invalid API Key for package = com.example.android.project. Status code received = 12
at android.os.Parcel.readException(Parcel.java:1693)
at android.os.Parcel.readException(Parcel.java:1646)
at com.google.android.gms.common.internal.zzax.zza(Unknown Source)
at com.google.android.gms.common.internal.zzd.zza(Unknown Source)
at com.google.android.gms.internal.zzbcj.zzpV(Unknown Source)
at com.google.android.gms.internal.zzbcn.run(Unknown Source)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at com.google.android.gms.internal.zzbgx.run(Unknown Source)
at java.lang.Thread.run(Thread.java:762)
这个错误没有意义,因为在开发者控制台中我将 API 密钥设置为没有限制,希望它能解决这个问题,即没有锁定到任何项目,它仍然无法工作。并且在控制台中启用了 API。
我尝试过其他线程的建议:无法连接到 Google API 客户端?,尝试连接 Google Awareness 服务时收到错误等。但问题仍然存在。
public class MainActivity extends AppCompatActivity implements
GoogleApiClient.OnConnectionFailedListener,
GoogleApiClient.ConnectionCallbacks {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Connect to Awareness API
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Awareness.API)
.build();
mGoogleApiClient.connect(); // the app crashes here
}
}
最后是我的 AndroidManifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.project">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/appicon"
android:label="@string/app_name"
android:roundIcon="@drawable/appicon"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.awareness.API_KEY"
android:value="MY_API_KEY"/>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="MY_API_KEY"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
我不知道该怎么办,有人有什么建议吗?
我正在三星 Galaxy S6 上进行测试,Android 版本:7.0