首页 > 解决方案 > Android API Google Drive“连接失败”

问题描述

我尝试在 Android 上使用 API Google Drive 但是,我遇到了无法解决的错误。

GoogleApiClient 连接失败:ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}

公共类 GoogleDriveBackup 实现备份,GoogleApiClient.OnConnectionFailedListener { @Nullable private GoogleApiClient googleApiClient;

@Nullable
private WeakReference<Activity> activityRef;


@Override
public void init(@NonNull final Activity activity) {
    this.activityRef = new WeakReference<>(activity);

    googleApiClient = new GoogleApiClient.Builder(activity)
            .addApi(Drive.API)
            .addScope(Drive.SCOPE_FILE)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    // Do nothing

                }

                @Override
                public void onConnectionSuspended(int i) {

                }
            })
            .addOnConnectionFailedListener(this)
            .build();
}

@Override
public GoogleApiClient getClient() {
    return googleApiClient;
}

@Override
public void start() {
    if (googleApiClient != null) {
        googleApiClient.connect();
    } else {
        throw new IllegalStateException("You should call init before start");
    }
}

@Override
public void stop() {
    if (googleApiClient != null) {
        googleApiClient.disconnect();
    } else {
        throw new IllegalStateException("You should call init before start");
    }
}

@Override
public void onConnectionFailed(@NonNull final ConnectionResult result) {
    Log.i("error", "GoogleApiClient connection failed: " + result.toString());

    if (result.hasResolution() && activityRef != null && activityRef.get() != null) {
        Activity a = activityRef.get();
        // show the localized error dialog.
        try {
            result.startResolutionForResult(a, 1);
        } catch (IntentSender.SendIntentException e) {
            FirebaseCrash.log("Drive connection failed");
            FirebaseCrash.report(e);
            e.printStackTrace();
            GoogleApiAvailability.getInstance().getErrorDialog(a, result.getErrorCode(), 0).show();
        }
    } else {
        Log.d("error", "cannot resolve connection issue");
    }
}

}

请帮忙

标签: android

解决方案


推荐阅读