@Test public void testParsing() throws IOException { final InputStream contents = getClass().getClassLoader().getResourceAsStream("youtube-search.json"); Preconditions.checkNotNull(contents); HttpTransport transport = new MockHttpTransport() { @Override public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { return new MockLowLevelHttpRequest() { @Override public LowLevelHttpResponse execute() throws IOException { MockLowLevelHttpResponse result = new MockLowLevelHttpResponse(); result.setContentType(Json.MEDIA_TYPE); result.setContent(contents); return result; } }; } }; HttpRequest request = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL); request.setParser(new JsonObjectParser(new GsonFactory())); HttpResponse response = request.execute(); YouTubeSample.ListResponse listResponse = YouTubeSample.parseJson(response); assertEquals(5, listResponse.getPageInfo().getResultsPerPage()); assertEquals(1000000, listResponse.getPageInfo().getTotalResults()); assertEquals(5, listResponse.getSearchResults().size()); for (YouTubeSample.SearchResult searchResult : listResponse.getSearchResults()) { assertEquals("youtube#searchResult", searchResult.getKind()); assertNotNull(searchResult.getId()); assertEquals("youtube#video", searchResult.getId().getKind()); assertNotNull(searchResult.getId().getVideoId()); YouTubeSample.Snippet snippet = searchResult.getSnippet(); assertNotNull(snippet); assertNotNull(snippet.getChannelId()); assertNotNull(snippet.getDescription()); assertNotNull(snippet.getTitle()); assertNotNull(snippet.getPublishedAt()); Map<String, YouTubeSample.Thumbnail> thumbnails = snippet.getThumbnails(); assertNotNull(thumbnails); for (Map.Entry<String, YouTubeSample.Thumbnail> entry : thumbnails.entrySet()) { assertNotNull(entry.getKey()); YouTubeSample.Thumbnail thumbnail = entry.getValue(); assertNotNull(thumbnail); assertNotNull(thumbnail.getUrl()); assertNotNull(thumbnail.getWidth()); assertNotNull(thumbnail.getHeight()); } } }
public static ListResponse parseJson(HttpResponse httpResponse) throws IOException { ListResponse listResponse = httpResponse.parseAs(ListResponse.class); if (listResponse.getSearchResults().isEmpty()) { System.out.println("No results found."); } else { for (SearchResult searchResult : listResponse.getSearchResults()) { System.out.println(); System.out.println("-----------------------------------------------"); System.out.println("Kind: " + searchResult.getKind()); System.out.println("Video ID: " + searchResult.getId().getVideoId()); System.out.println("Title: " + searchResult.getSnippet().getTitle()); System.out.println("Description: " + searchResult.getSnippet().getDescription()); } } return listResponse; }
YouTubeSample { public static ListResponse parseJson(HttpResponse httpResponse) throws IOException { ListResponse listResponse = httpResponse.parseAs(ListResponse.class); if (listResponse.getSearchResults().isEmpty()) { System.out.println("No results found."); } else { for (SearchResult searchResult : listResponse.getSearchResults()) { System.out.println(); System.out.println("-----------------------------------------------"); System.out.println("Kind: " + searchResult.getKind()); System.out.println("Video ID: " + searchResult.getId().getVideoId()); System.out.println("Title: " + searchResult.getSnippet().getTitle()); System.out.println("Description: " + searchResult.getSnippet().getDescription()); } } return listResponse; } }
YouTubeSample { public static ListResponse parseJson(HttpResponse httpResponse) throws IOException { ListResponse listResponse = httpResponse.parseAs(ListResponse.class); if (listResponse.getSearchResults().isEmpty()) { System.out.println("No results found."); } else { for (SearchResult searchResult : listResponse.getSearchResults()) { System.out.println(); System.out.println("-----------------------------------------------"); System.out.println("Kind: " + searchResult.getKind()); System.out.println("Video ID: " + searchResult.getId().getVideoId()); System.out.println("Title: " + searchResult.getSnippet().getTitle()); System.out.println("Description: " + searchResult.getSnippet().getDescription()); } } return listResponse; }  }
YouTubeSample { public static ListResponse parseJson(HttpResponse httpResponse) throws IOException { ListResponse listResponse = httpResponse.parseAs(ListResponse.class); if (listResponse.getSearchResults().isEmpty()) { System.out.println("No results found."); } else { for (SearchResult searchResult : listResponse.getSearchResults()) { System.out.println(); System.out.println("-----------------------------------------------"); System.out.println("Kind: " + searchResult.getKind()); System.out.println("Video ID: " + searchResult.getId().getVideoId()); System.out.println("Title: " + searchResult.getSnippet().getTitle()); System.out.println("Description: " + searchResult.getSnippet().getDescription()); } } return listResponse; }  static ListResponse parseJson(HttpResponse httpResponse); }
YouTubeSample { public static ListResponse parseJson(HttpResponse httpResponse) throws IOException { ListResponse listResponse = httpResponse.parseAs(ListResponse.class); if (listResponse.getSearchResults().isEmpty()) { System.out.println("No results found."); } else { for (SearchResult searchResult : listResponse.getSearchResults()) { System.out.println(); System.out.println("-----------------------------------------------"); System.out.println("Kind: " + searchResult.getKind()); System.out.println("Video ID: " + searchResult.getId().getVideoId()); System.out.println("Title: " + searchResult.getSnippet().getTitle()); System.out.println("Description: " + searchResult.getSnippet().getDescription()); } } return listResponse; }  static ListResponse parseJson(HttpResponse httpResponse);  }