最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - List.execute() of API Google Drive doesn't work - Stack Overflow

programmeradmin3浏览0评论

I'm trying to implement Google Drive backup functionality in my Android app, written in Java, but when calling execute(), the code in debug mode doesn't proceed and keeps running indefinitely, as if it got lost in the ether.

This is my method:

private static boolean isFilePresentInAppDataFolder(Drive driveService, String fileName) {
        try {

            String query = "'appDataFolder' in parents and name = '" + fileName + "'";
            List result = driveService.files().list()
                    .setQ(query)
                    .setSpaces("appDataFolder")
                    .setFields("files(id, name)")
                    .setPageSize(1);

            FileList executed = result.execute();

            if (executed.getFiles().isEmpty()) {
                return false;
            } else {
                return true;
            }

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}

It is throw on a different thread with Executors. I had configured my project on Google Cloud, with SHA-1 from android terminal with signinReport task and with Google Drive API activated.

The issue seems to persist even after ensuring that the necessary permissions are granted. Additionally, I'm not seeing any activity in the Google Cloud Console dashboard, suggesting that no connection attempts are being logged from my app. Despite debugging the authentication and permission processes, the execution gets stuck without any clear error. I'm unsure whether this is related to running the app in debug mode, as the system logs don’t provide much insight into what might be failing. I've attempted to isolate Google’s logs but haven't identified any specific issues yet, and I am looking for suggestions on how to troubleshoot this more effectively.

I signIn correctly, during debug I see my google account correctly:

public static void singIn(Activity activity, int reqCode){

    GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestScopes(new Scope(DriveScopes.DRIVE_APPDATA))
            .build();

    GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(activity, signInOptions);

    Intent signInIntent = googleSignInClient.getSignInIntent();
    activity.startActivityForResult(signInIntent, reqCode);
}

Furthermore, this is my method to get drive service:

public static Drive getDriveService(Context ctx) throws GeneralSecurityException, IOException {

    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(ctx);
    if (account == null) {
        throw new IllegalStateException("Utente non loggato su Google. Chiamare signIn() prima di accedere a Drive.");
    }

    GoogleAccountCredential credential =
            GoogleAccountCredential.usingOAuth2(ctx, Arrays.asList(DriveScopes.DRIVE_APPDATA))
                    .setBackOff(new ExponentialBackOff());
    credential.setSelectedAccount(account.getAccount());

    HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

    return new Drive.Builder(transport, jsonFactory, request -> {
        credential.initialize(request);
        request.setLoggingEnabled(true);
    }).setApplicationName(Tools.APP_NAME /*my app name*/).build();
}

What can I try next?

发布评论

评论列表(0)

  1. 暂无评论