ระยะหมดเวลาและข้อผิดพลาด
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เอกสารนี้อธิบายวิธีตั้งระยะหมดเวลาและจัดการข้อผิดพลาด HTTP ที่โค้ดของคุณ
อาจได้รับเมื่อคุณใช้ไลบรารีของไคลเอ็นต์ Google API สำหรับ Java
เนื้อหา
การตั้งค่าระยะหมดเวลา
ในตัวอย่างต่อไปนี้ ซึ่งใช้ 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();
การจัดการการตอบกลับข้อผิดพลาด HTTP จาก Google APIs
เมื่อมีการตรวจพบรหัสสถานะข้อผิดพลาดในการตอบกลับ HTTP ไปยัง Google API ที่
ใช้รูปแบบ JSON ไลบรารีที่สร้างขึ้นจะมี GoogleJsonResponseException
ข้อผิดพลาดใช้รูปแบบที่ระบุไว้ในการตอบกลับข้อผิดพลาด
ตัวอย่างต่อไปนี้แสดงวิธีหนึ่งที่คุณสามารถจัดการข้อยกเว้นเหล่านี้ได้
Drive.Files.List listFiles = drive.files.list();
try {
FileList response = listFiles.execute();
...
} catch (GoogleJsonResponseException e) {
System.err.println(e.getDetails());
}
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 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,[]]