Hết thời gian chờ và lỗi
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Tài liệu này mô tả cách đặt thời gian chờ và xử lý các lỗi HTTP mà mã của bạn
có thể nhận được khi bạn dùng Thư viện ứng dụng API của Google cho Java.
Nội dung
Đặt thời gian chờ
Trong ví dụ sau đây, việc sử dụng API Google Analytics,
Các phương thức setConnectTimeout
và setReadTimeout
dùng để thiết lập phương thức kết nối và
thời gian chờ đọc thành 3 phút (tính bằng mili giây) cho tất cả các yêu cầu:
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();
Xử lý phản hồi lỗi HTTP từ các API của Google
Khi phát hiện mã trạng thái lỗi trong phản hồi HTTP tới một API của Google
sử dụng định dạng JSON, nên các thư viện được tạo sẽ gửi một GoogleJsonResponseException.
Các lỗi sử dụng định dạng được chỉ định trong phần Phản hồi lỗi.
Ví dụ sau cho thấy một cách mà bạn có thể xử lý các ngoại lệ này:
Drive.Files.List listFiles = drive.files.list();
try {
FileList response = listFiles.execute();
...
} catch (GoogleJsonResponseException e) {
System.err.println(e.getDetails());
}
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-31 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\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 }"]]