[ Web Proxy ]
URL:
Viewing: https://developers.google.com/api-client-library/java/google-api-java-client/errors [Back]  [Original]

Timeouts and Errors  |  API Client Library for Java  |  Google for Developers Skip to main content

Timeouts and Errors Stay organized with collections Save and categorize content based on your preferences.

outlined_flag

This document describes how to set timeouts and handle HTTP errors that your code might receive when you use the Google API Client Library for Java.

Contents

Setting timeouts

In the following example, which uses the Google Analytics API, the setConnectTimeout and setReadTimeout methods are used to set the connect and read timeouts to three minutes (in milliseconds) for all requests:

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();

Handling HTTP error responses from Google APIs

When an error status code is detected in an HTTP response to a Google API that uses the JSON format, the generated libraries throw a GoogleJsonResponseException.

The errors use the format specified in Error responses.

The following example shows one way that you can handle these exceptions:

Drive.Files.List listFiles = drive.files.list();
try {
  FileList response = listFiles.execute();
  ...
} catch (GoogleJsonResponseException e) {
  System.err.println(e.getDetails());
}

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-07-16 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-07-16 UTC."],[],[]]

Web Proxy Viewer  |  New URL  |  Original Page