Google HTTP Client for Java

Overview
Setup Instructions
Component Modules
Android
Google App Engine
HTTP Transport
JSON
Exponential Backoff
Unit Testing
Support

JSON

Pluggable streaming library

A fully pluggable JSON streaming library abstraction allows you to take advantage of the native platform’s built-in JSON library support (for example the JSON library that is built into Android Honeycomb). The streaming library enables you to write optimized code for efficient memory usage that minimizes parsing and serialization time.

A big advantage of this JSON library is that the choice of low-level streaming library is fully pluggable. There are three built-in choices, all of which extend JsonFactory. You can easily plug in your own implementation.

User-defined JSON data models

User-defined JSON data models allow you to define Plain Old Java Objects (POJOs) and define how the library parses and serializes them to and from JSON. The code snippets below are part of a more complete example, YouTube sample, which demonstrates these concepts.

Example

The following JSON snippet shows the relevant fields of a typical YouTube video search:

{
 "kind": "youtube#searchListResponse",
 "pageInfo": {
  "totalResults": 1000000,
  "resultsPerPage": 5
 },
 "items": [
  {
   "kind": "youtube#searchResult",
   "id": {
    "kind": "youtube#video",
    "videoId": "e6Tudp5lqt8"
   },
   "snippet": {
    "publishedAt": "2020-06-25T23:18:43Z",
    "channelId": "UCKwGZZMrhNYKzucCtTPY2Nw",
    "title": "Video 1 Title",
    "description": "Video 1 Description",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/e6Tudp5lqt8/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/e6Tudp5lqt8/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/e6Tudp5lqt8/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    }
   }
  },
  {
   "kind": "youtube#searchResult",
   "id": {
    "kind": "youtube#video",
    "videoId": "o-NtLpiMpw0"
   },
   "snippet": {
    "publishedAt": "2020-06-25T17:28:52Z",
    "channelId": "UClljAz6ZKy0XeViKsohdjqA",
    "title": "Video Title 2",
    "description": "Video 2 Description",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/o-NtLpiMpw0/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/o-NtLpiMpw0/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/o-NtLpiMpw0/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    }
   }
  },
  {
   "kind": "youtube#searchResult",
   "id": {
    "kind": "youtube#video",
    "videoId": "TPAahzXZFZo"
   },
   "snippet": {
    "publishedAt": "2020-06-26T15:45:00Z",
    "channelId": "UCR4Yfr8HAZJd9X24dwuAt1Q",
    "title": "Video 3 Title",
    "description": "Video 3 Description",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/TPAahzXZFZo/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/TPAahzXZFZo/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/TPAahzXZFZo/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    }
   }
  },
  {
   "kind": "youtube#searchResult",
   "id": {
    "kind": "youtube#video",
    "videoId": "gBL-AelsdFk"
   },
   "snippet": {
    "publishedAt": "2020-06-24T15:24:06Z",
    "channelId": "UCFHZHhZaH7Rc_FOMIzUziJA",
    "title": "Video 4 Title",
    "description": "Video 4 Description",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/gBL-AelsdFk/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/gBL-AelsdFk/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/gBL-AelsdFk/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    }
   }
  },
  {
   "kind": "youtube#searchResult",
   "id": {
    "kind": "youtube#video",
    "videoId": "9ofe8axKjH0"
   },
   "snippet": {
    "publishedAt": "2020-06-26T11:59:32Z",
    "channelId": "UCtNpbO2MtsVY4qW23WfnxGg",
    "title": "Video 5 Title",
    "description": "Video 5 Description",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/9ofe8axKjH0/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/9ofe8axKjH0/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/9ofe8axKjH0/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    }
   }
  }
 ]
}

Here’s one possible way to design the Java data classes to represent this:

public static class ListResponse {
  @Key("items")
  private List<SearchResult> searchResults;

  @Key
  private PageInfo pageInfo;

  public List<SearchResult> getSearchResults() {
    return searchResults;
  }

  public PageInfo