Skip to content

Commit d936889

Browse files
author
hgomez
committed
first version
1 parent 73ae3cf commit d936889

6 files changed

Lines changed: 164 additions & 0 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,21 @@
622622
<artifactId>bcpkix-jdk15on</artifactId>
623623
<version>1.58</version>
624624
</dependency>
625+
<dependency>
626+
<groupId>com.google.http-client</groupId>
627+
<artifactId>google-http-client</artifactId>
628+
<version>1.23.0</version>
629+
</dependency>
630+
<dependency>
631+
<groupId>com.google.http-client</groupId>
632+
<artifactId>google-http-client-jackson2</artifactId>
633+
<version>1.23.0</version>
634+
</dependency>
635+
<dependency>
636+
<groupId>com.google.http-client</groupId>
637+
<artifactId>google-http-client-gson</artifactId>
638+
<version>1.23.0</version>
639+
</dependency>
625640
</dependencies>
626641
<repositories>
627642
<repository>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.GenericUrl;
9+
import com.google.api.client.util.Key;
10+
11+
/**
12+
*
13+
* @author Hugo
14+
*/
15+
public class DailyMotionUrl extends GenericUrl {
16+
17+
public DailyMotionUrl(String encodedUrl) {
18+
super(encodedUrl);
19+
}
20+
21+
@Key public String fields;
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.util.Key;
9+
import java.util.List;
10+
11+
/**
12+
*
13+
* @author Hugo
14+
*/
15+
public class Video {
16+
@Key public String id;
17+
18+
@Key public List<String> tags;
19+
20+
@Key public String title;
21+
22+
@Key public String url;
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.util.Key;
9+
import java.util.List;
10+
11+
/**
12+
*
13+
* @author Hugo
14+
*/
15+
public class VideoFeed {
16+
@Key public List<Video> list;
17+
18+
@Key("has_more")
19+
public boolean hasMore;
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Properties file which configures the operation of the JDK logging facility.
2+
# The system will look for this config file to be specified as a system property:
3+
# -Djava.util.logging.config.file=${project_loc:dailymotion-simple-cmdline-sample}/logging.properties
4+
5+
# Set up the console handler (uncomment "level" to show more fine-grained messages)
6+
handlers = java.util.logging.ConsoleHandler
7+
java.util.logging.ConsoleHandler.level = ALL
8+
9+
# Set up logging of HTTP requests and responses (uncomment "level" to show)
10+
com.google.api.client.http.level = ALL

0 commit comments

Comments
 (0)