टाइम आउट और गड़बड़ियां
संग्रह की मदद से व्यवस्थित रहें
अपनी प्राथमिकताओं के आधार पर, कॉन्टेंट को सेव करें और कैटगरी में बांटें.
इस दस्तावेज़ में टाइम आउट सेट करने और उन एचटीटीपी गड़बड़ियों को हैंडल करने के बारे में बताया गया है जो आपके कोड से जुड़ी हैं
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 से एचटीटीपी गड़बड़ी के रिस्पॉन्स मैनेज करना
जब Google API के एचटीटीपी रिस्पॉन्स में गड़बड़ी की स्थिति वाले कोड का पता चलता है
JSON फ़ॉर्मैट का इस्तेमाल करता है, तो जनरेट की गई लाइब्रेरी GoogleJsonResponseException देती है.
गड़बड़ियां, गड़बड़ी के रिस्पॉन्स में बताए गए फ़ॉर्मैट का इस्तेमाल करती हैं.
नीचे दिए गए उदाहरण में, इन अपवादों को मैनेज करने का तरीका बताया गया है:
Drive.Files.List listFiles = drive.files.list();
try {
FileList response = listFiles.execute();
...
} catch (GoogleJsonResponseException e) {
System.err.println(e.getDetails());
}
जब तक कुछ अलग से न बताया जाए, तब तक इस पेज की सामग्री को Creative Commons Attribution 4.0 License के तहत और कोड के नमूनों को Apache 2.0 License के तहत लाइसेंस मिला है. ज़्यादा जानकारी के लिए, Google Developers साइट नीतियां देखें. Oracle और/या इससे जुड़ी हुई कंपनियों का, Java एक रजिस्टर किया हुआ ट्रेडमार्क है.
आखिरी बार 2025-07-26 (UTC) को अपडेट किया गया.
[null,null,["आखिरी बार 2025-07-26 (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,["This 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\nSetting timeouts\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\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 }"]]