逾時與錯誤
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本文件說明如何設定逾時,以及處理程式碼發生的 HTTP 錯誤
使用適用於 Java 的 Google API 用戶端程式庫時,可能會收到的訊息。
目錄
設定逾時
以下範例使用 Google Analytics API,
setConnectTimeout
和 setReadTimeout
方法用於設定連線及
所有要求的讀取逾時時間至三分鐘 (以毫秒為單位):
private 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
}
};
GoogleCredential credential = ....
final Analytics analytics = Analytics.builder(new NetHttpTransport(), jsonFactory, setHttpTimeout(credential)).build();
處理來自 Google API 的 HTTP 錯誤回應
在對 Google API 發出的 HTTP 回應中偵測到錯誤狀態碼時
使用 JSON 格式,產生的程式庫會擲回 GoogleJsonResponseException。
錯誤會使用錯誤回應中指定的格式。
以下範例說明處理這些例外狀況的方法:
Drive.Files.List listFiles = drive.files.list();
try {
FileList response = listFiles.execute();
...
} catch (GoogleJsonResponseException e) {
System.err.println(e.getDetails());
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eThis guide explains how to configure timeout settings for HTTP requests when using the Google API Client Library for Java, ensuring your application handles potential delays.\u003c/p\u003e\n"],["\u003cp\u003eIt demonstrates how to catch and manage HTTP error responses, specifically \u003ccode\u003eGoogleJsonResponseException\u003c/code\u003e, which occur when interacting with Google APIs.\u003c/p\u003e\n"],["\u003cp\u003eThe content provides code examples for setting connection and read timeouts and illustrates how to handle errors using try-catch blocks with \u003ccode\u003eGoogleJsonResponseException\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# Timeouts and Errors\n\nThis document describes how to set timeouts and handle HTTP errors that your code\nmight receive when you use the Google API Client Library for Java.\n\nContents\n--------\n\nSetting timeouts\n----------------\n\nIn the following example, which uses the [Google Analytics API](https://developers.google.com/api-client-library/java/apis/analytics/v3), the\n`setConnectTimeout` and `setReadTimeout` methods are used to set the connect and\nread timeouts to three minutes (in milliseconds) for all requests: \n\n private HttpRequestInitializer setHttpTimeout(final HttpRequestInitializer requestInitializer) {\n return new HttpRequestInitializer() {\n @Override\n public void initialize(HttpRequest httpRequest) throws IOException {\n requestInitializer.initialize(httpRequest);\n httpRequest.setConnectTimeout(3 * 60000); // 3 minutes connect timeout\n httpRequest.setReadTimeout(3 * 60000); // 3 minutes read timeout\n }\n };\n\n GoogleCredential credential = ....\n\n final Analytics analytics = Analytics.builder(new NetHttpTransport(), jsonFactory, setHttpTimeout(credential)).build();\n\nHandling HTTP error responses from Google APIs\n----------------------------------------------\n\nWhen an error status code is detected in an HTTP response to a Google API that\nuses the JSON format, the generated libraries throw a [GoogleJsonResponseException](https://googleapis.dev/java/google-api-client/latest/com/google/api/client/googleapis/json/GoogleJsonResponseException.html).\n\nThe errors use the format specified in [Error responses](https://cloud.google.com/apis/design/errors).\n\nThe following example shows one way that you can handle these exceptions: \n\n Drive.Files.List listFiles = drive.files.list();\n try {\n FileList response = listFiles.execute();\n ...\n } catch (GoogleJsonResponseException e) {\n System.err.println(e.getDetails());\n }"]]