Dav*_*Vdd 4 java google-api google-drive-api
我有一个驱动器应用程序,该应用程序请求所有未被回收的文件。但是有时它会抛出带有读取超时的IOexception。有办法避免这种情况吗?
这是我得到的错误:
发生错误:java.net.SocketTimeoutException:读取超时
也许我的指数补偿实施错误。
这是我用来获取文件的代码:
private static List<File> retrieveAllNoTrashFiles(Drive service) throws IOException, InterruptedException {
List<File> result = new ArrayList<File>();
Files.List request = service.files().list().setQ("trashed = false").setMaxResults(1000);
do {
try {
FileList files =executeRequest(service,request);
result.addAll(files.getItems());
request.setPageToken(files.getNextPageToken());
} catch (IOException e) { //here I sometimes get the read timeout
System.out.println("An error occurred: " + e);
request.setPageToken(null);
}
} while (request.getPageToken() != null
&& request.getPageToken().length() > 0);
return result;
}
private static FileList executeRequest(Drive service,Files.List request) throws IOException, InterruptedException {
Random randomGenerator = new Random();
for (int n = 0; n < 5; ++n) {
try {
return(request.execute());
} catch (GoogleJsonResponseException e) {
if (e.getDetails().getCode() == 403
&& (e.getDetails().getErrors().get(0).getReason().equals("rateLimitExceeded")
|| e.getDetails().getErrors().get(0).getReason().equals("userRateLimitExceeded"))) {
// Apply exponential backoff.
Thread.sleep((1 << n) * 1000 + randomGenerator.nextInt(1001));
}
//else {
// Other error, re-throw.
// throw e;
// }
}
}catch(SocketTimeoutException e){
Thread.sleep((1 << n) * 1000 + randomGenerator.nextInt(1001));
}
System.err.println("There has been an error, the request never succeeded.");
return null;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
几天前我也有同样的经历。在这里找到了我的问题的答案。https://code.google.com/p/google-api-java-client/wiki/FAQ。创建云端硬盘实例时,可以调用setHttpRequestInitilalizer方法,将新的HttpRequestInitializer实例作为参数传递,并实现initialize方法。在其中,您可以增加ReadTimeout和ConnectionTimeout。
这是一个示例代码:
Drive drive = new Drive.Builder(this.httpTransport, this.jsonFactory, this.credential).setHttpRequestInitializer(new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
credential.initialize(httpRequest);
httpRequest.setConnectTimeout(300 * 60000); // 300 minutes connect timeout
httpRequest.setReadTimeout(300 * 60000); // 300 minutes read timeout
}
}).setApplicationName("My Application").build();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3858 次 |
| 最近记录: |