我使用的是drive REST Api (不是Google Drive Android API),当我向Google Drive发送请求时,经常会遇到超时(+- each 5 request)。它可以在我列出文件夹或下载文件时出现,但仅当我发出请求时才会出现。在下载过程中决不会。
对于测试,我使用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-out2.
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)有什么想法吗?
发布于 2019-03-13 10:45:43
对于其他有这个问题的人,这对我很有效:
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
发布于 2015-11-23 07:19:33
我在你的代码中看不到任何明显的错误,事实上,我已经成功地使用了非常类似的结构:
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/ i未指定'.setBackOff(new ExponentialBackOff())‘策略
2/ i使用“activity.getApplicationContext()”代替activity context
3/我不知道您的用户电子邮件(accountName)来自何处。它必须是在设备上注册的帐户之一,可以通过以下方式实现:
第3/点很可能不会导致您的问题,它将返回不同的错误消息。
所有这些都可以在RESTDemo here中看到(参见REST.init()和REQ_ACCPICK)。
祝好运
https://stackoverflow.com/questions/33853978
复制相似问题