|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package com.baeldung.googlehttpclientguide; |
| 7 | + |
| 8 | +import com.google.api.client.http.HttpRequest; |
| 9 | +import com.google.api.client.http.HttpRequestFactory; |
| 10 | +import com.google.api.client.http.HttpRequestInitializer; |
| 11 | +import com.google.api.client.http.HttpResponseException; |
| 12 | +import com.google.api.client.http.HttpTransport; |
| 13 | +import com.google.api.client.http.javanet.NetHttpTransport; |
| 14 | +import com.google.api.client.json.JsonFactory; |
| 15 | +import com.google.api.client.json.JsonObjectParser; |
| 16 | +import com.google.api.client.json.gson.GsonFactory; |
| 17 | +import com.google.api.client.json.jackson2.JacksonFactory; |
| 18 | + |
| 19 | +/** |
| 20 | + * |
| 21 | + * @author Hugo |
| 22 | + */ |
| 23 | +public class DailyMotionExample { |
| 24 | + |
| 25 | + static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); |
| 26 | + static final JsonFactory JSON_FACTORY = new JacksonFactory(); |
| 27 | + //static final JsonFactory JSON_FACTORY = new GsonFactory(); |
| 28 | + |
| 29 | + private static void run() throws Exception { |
| 30 | + HttpRequestFactory requestFactory |
| 31 | + = HTTP_TRANSPORT.createRequestFactory( |
| 32 | + new HttpRequestInitializer() { |
| 33 | + |
| 34 | + public void initialize(HttpRequest request) { |
| 35 | + request.setParser(new JsonObjectParser(JSON_FACTORY)); |
| 36 | + } |
| 37 | + }); |
| 38 | + DailyMotionUrl url = new DailyMotionUrl("https://api.dailymotion.com/videos/"); |
| 39 | + url.fields = "id,tags,title,url"; |
| 40 | + HttpRequest request = requestFactory.buildGetRequest(url); |
| 41 | + VideoFeed videoFeed = request.execute().parseAs(VideoFeed.class); |
| 42 | + if (videoFeed.list.isEmpty()) { |
| 43 | + System.out.println("No videos found."); |
| 44 | + } else { |
| 45 | + if (videoFeed.hasMore) { |
| 46 | + System.out.print("First "); |
| 47 | + } |
| 48 | + System.out.println(videoFeed.list.size() + " videos found:"); |
| 49 | + for (Video video : videoFeed.list) { |
| 50 | + System.out.println(); |
| 51 | + System.out.println("-----------------------------------------------"); |
| 52 | + System.out.println("ID: " + video.id); |
| 53 | + System.out.println("Title: " + video.title); |
| 54 | + System.out.println("Tags: " + video.tags); |
| 55 | + System.out.println("URL: " + video.url); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public static void main(String[] args) { |
| 61 | + try { |
| 62 | + try { |
| 63 | + run(); |
| 64 | + return; |
| 65 | + } catch (HttpResponseException e) { |
| 66 | + System.err.println(e.getMessage()); |
| 67 | + } |
| 68 | + } catch (Throwable t) { |
| 69 | + t.printStackTrace(); |
| 70 | + } |
| 71 | + System.exit(1); |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments