Google Drive REST API连接TimeOut?

时间:2015-11-22 10:53:47

标签: android rest google-drive-api timeout

我正在使用Drive REST Api(不是Google Drive Android API),当我向Google云端硬盘发送请求时,我经常会超时(+ - 每5个请求)。它可以在我列出文件夹或下载文件时出现,但仅在我发出请求时出现。从未下载过。

对于测试,我使用Wifi连接。没有问题的互联网连接。

我在文档(https://developers.google.com/drive/web/quickstart/android)中创建了类似描述的连接。这是代码:

googleAccountCredential = GoogleAccountCredential.usingOAuth2(activity, Arrays.asList(DriveScopes.DRIVE_FILE));

if (accountName.length() == 0) {
    activity.startActivityForResult(googleAccountCredential.newChooseAccountIntent(), ResultCode.GOOGLE_DRIVE_ACCOUNT_NAME_RESULT_CODE);
    return false;
} else {
    if (!isConnected) {
        googleAccountCredential.setSelectedAccountName(accountName);
        googleAccountCredential.setBackOff(new ExponentialBackOff());

        driveService = new Drive.Builder(AndroidHttp.newCompatibleTransport(), JacksonFactory.getDefaultInstance(), googleAccountCredential)
                .setApplicationName(activity.getString(R.string.app_name))
                .build();

        isConnected = true;
    }

    return true;
}

所以,如果我没有“帐户名”,我会问它,否则我只创建一次连接! (我每次尝试创建连接,但它不会改变任何东西)

所有请求都是在asyncTask中进行的。

有时它可以工作,有时候不工作。

请求超时例子:

1

request = driveService.files().list();
request.setQ("root in parents AND trashed = false");
fileList = request.execute();// Time-out

2

HttpResponse resp = driveService.getRequestFactory().buildGetRequest(new GenericUrl(url)).execute();// Time-out

编辑:

这是转储:

W/System.err: java.net.SocketTimeoutException: timeout
W/System.err: at com.android.okhttp.okio.Okio$3.newTimeoutException(Okio.java:207)
W/System.err: at com.android.okhttp.okio.AsyncTimeout.exit(AsyncTimeout.java:250)
W/System.err: at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:217)
W/System.err: at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
W/System.err: at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
W/System.err: at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
W/System.err: at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)
W/System.err: at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:904)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:788)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:439)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java)
W/System.err: at com.google.api.client.http.javanet.NetHttpResponse.<init>(NetHttpResponse.java:37)
W/System.err: at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:94)
W/System.err: at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:972)
W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
W/System.err: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
W/System.err: at org.team.acs.scubalog.share.cloud.googleDrive.asynckTask.GoogleDriveGetDirectoryListAsyncTask.doInBackground(GoogleDriveGetDirectoryListAsyncTask.java:60)
W/System.err: at org.team.acs.scubalog.share.cloud.googleDrive.asynckTask.GoogleDriveGetDirectoryListAsyncTask.doInBackground(GoogleDriveGetDirectoryListAsyncTask.java:20)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err: at java.lang.Thread.run(Thread.java:818)

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我的代码中没有看到任何明显不正确的内容,事实上,我已经成功使用了非常类似的构造:

  com.google.api.services.drive.Drive driveService =
    new Drive.Builder(
      AndroidHttp.newCompatibleTransport(),
      new GsonFactory(),
      GoogleAccountCredential
        .usingOAuth2(activity.getApplicationContext(), Collections.singletonList(DriveScopes.DRIVE_FILE))
        .setSelectedAccountName(accountName)
  ).build();

所以,让我们看看差异(不是说这是修复):

1 /我没有指定'.setBackOff(new ExponentialBackOff())'策略

2 /我使用'activity.getApplicationContext()'而不是活动上下文

3 /我不知道您的用户电子邮件(accountName)来自哪里。它必须是在设备上注册的帐户之一,可以通过以下方式实现:

  • 标准Android设置&gt;帐户菜单&gt;添加帐户
  • 未指定'setSelectedAccountName(accountName)',这会导致帐户选择器弹出,允许用户选择已注册的帐户或添加新帐户
  • 在您的活动中调用帐户选择器并从'onActivityResult'
  • 获取电子邮件

第3点很可能不会导致您的问题,它会返回不同的错误消息。

所有这一切都可以在RESTDemo here中看到(请参阅REST.init()REQ_ACCPICK)。

祝你好运

答案 1 :(得分:0)

对于其他有此问题的人,这对我有用:

    private static HttpRequestInitializer setHttpTimeout(final HttpRequestInitializer requestInitializer) {
            return new HttpRequestInitializer() {
                @Override
                public void initialize(HttpRequest httpRequest) throws IOException {
                    requestInitializer.initialize(httpRequest);
                    httpRequest.setConnectTimeout(3 * 60000);  // 3 minutes connect timeout
                    httpRequest.setReadTimeout(3 * 60000);  // 3 minutes read timeout
                }
            };
        }


 @Nullable
    public static DriveServiceHelper create(Context context) {

        Set<Scope> requiredScopes = new HashSet<>(2);
        requiredScopes.add(new Scope(DriveScopes.DRIVE_APPDATA));
        requiredScopes.add(new Scope(DriveScopes.DRIVE_FILE));

        GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(context);
        if (signInAccount != null && signInAccount.getGrantedScopes().containsAll(requiredScopes)) {




            // Use the authenticated account to sign in to the Drive service.
            GoogleAccountCredential credential =
                    GoogleAccountCredential.usingOAuth2(context, Collections.singleton(DriveScopes.DRIVE_APPDATA));
            credential.setSelectedAccount(signInAccount.getAccount());
            com.google.api.services.drive.Drive googleDriveService =
                    new com.google.api.services.drive.Drive.Builder(
                            AndroidHttp.newCompatibleTransport(),
                            new GsonFactory(),
                            setHttpTimeout(credential))
                            .setApplicationName(context.getString(R.string.app_name))
                            .build();

            return new DriveServiceHelper(googleDriveService);
        }

        return null;
    }

参考: https://developers.google.com/api-client-library/java/google-api-java-client/errors