我正在尝试将Android的Google Drive API
与我的Android应用程序集成。
我的大四在Google Developer Console上使用应用包名称和我的计算机上的SHA1
创建了一个项目,并生成了OAuth 2.0
客户端ID(我无法访问该帐户)。
点击按钮,我必须让用户访问Drive上的文件,以便将它们上传到服务器。
我调用以下代码onClick()
:
mGoogleApiClient = new GoogleApiClient.Builder(Activity.this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(Activity.this)
.addOnConnectionFailedListener(Activity.this)
.build();
mGoogleApiClient.connect();
这会调用onConnectionFailed()
回调:
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "Connection failed");
if (connectionResult.hasResolution()) {
try {
Log.i(TAG, "Connection failed - Trying again");
connectionResult.startResolutionForResult(this, RESOLVE_CONNECTION_REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
// Unable to resolve, message user appropriately
}
} else {
Log.i(TAG, "Connection failed, Code : " + connectionResult.getErrorCode());
GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show();
}
}
连接失败并再次尝试。在重试时,它会调用onActivityResult()
并尝试再次连接:
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
if (requestCode == Constants.RESOLVE_CONNECTION_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Log.i(TAG, "Code match");
mGoogleApiClient.connect();
}
}
此次连接尝试失败,错误代码为8,并显示消息"应用程序在使用Google Play服务时遇到问题。如果问题仍然存在,请联系开发人员寻求帮助。"
我也读到了这个: <Your App> is having trouble with Google Play services - When using GoogleApiClient, it always trigger onConnectionFailed,但没有帮助。
我尝试在谷歌上查找,但还没有任何帮助。任何帮助都会很棒!
答案 0 :(得分:0)
尽量不要在点击mGoogleApiClient
功能中设置onClick()
。如果您选中此android-demo和此drive quickstart,则会在onResume()
生命周期中实例化mGoogleApiClient:
@Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
// Create the API client and bind it to an instance variable.
// We use this instance as the callback for connection and connection
// failures.
// Since no account name is passed, the user is prompted to choose.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
// Connect the client. Once connected, the camera is launched.
mGoogleApiClient.connect();
}
查看官方Android Drive API了解详情。