33import com .google .api .client .http .HttpBackOffUnsuccessfulResponseHandler ;
44import com .google .api .client .http .HttpRequest ;
55import com .google .api .client .http .HttpRequestFactory ;
6+ import com .google .api .client .http .HttpResponse ;
67import com .google .api .client .http .HttpResponseException ;
78import com .google .api .client .http .HttpTransport ;
9+ import com .google .api .client .http .apache .ApacheHttpTransport ;
810import com .google .api .client .http .javanet .NetHttpTransport ;
911import com .google .api .client .json .JsonFactory ;
1012import com .google .api .client .json .JsonObjectParser ;
1113import com .google .api .client .json .jackson2 .JacksonFactory ;
1214import com .google .api .client .util .ExponentialBackOff ;
15+ import com .google .gson .reflect .TypeToken ;
16+ import java .lang .reflect .Type ;
17+ import java .util .List ;
18+ import java .util .concurrent .ExecutorService ;
19+ import java .util .concurrent .Executors ;
20+ import java .util .concurrent .Future ;
1321
14- public class DailyMotionExample {
15-
22+ public class GitHubExample {
1623 static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport ();
24+ //static final HttpTransport HTTP_TRANSPORT = new ApacheHttpTransport();
1725 static final JsonFactory JSON_FACTORY = new JacksonFactory ();
1826 //static final JsonFactory JSON_FACTORY = new GsonFactory();
1927
@@ -23,8 +31,9 @@ private static void run() throws Exception {
2331 (HttpRequest request ) -> {
2432 request .setParser (new JsonObjectParser (JSON_FACTORY ));
2533 });
26- DailyMotionUrl url = new DailyMotionUrl ("https://api.dailymotion.com/videos/" );
27- url .fields = "id,tags,title,url" ;
34+ GitHubUrl url = new GitHubUrl ("https://api.github.com/users" );
35+ url .per_page = 10 ;
36+ url .page = 1 ;
2837 HttpRequest request = requestFactory .buildGetRequest (url );
2938 ExponentialBackOff backoff = new ExponentialBackOff .Builder ()
3039 .setInitialIntervalMillis (500 )
@@ -34,37 +43,23 @@ private static void run() throws Exception {
3443 .setRandomizationFactor (0.5 )
3544 .build ();
3645 request .setUnsuccessfulResponseHandler (new HttpBackOffUnsuccessfulResponseHandler (backoff ));
37- VideoFeed videoFeed = request .execute ().parseAs (VideoFeed .class );
38- if (videoFeed .list .isEmpty ()) {
39- System .out .println ("No videos found." );
40- } else {
41- if (videoFeed .hasMore ) {
42- System .out .print ("First " );
43- }
44- System .out .println (videoFeed .list .size () + " videos found:" );
45- for (Video video : videoFeed .list ) {
46- System .out .println ();
47- System .out .println ("-----------------------------------------------" );
48- System .out .println ("ID: " + video .id );
49- System .out .println ("Title: " + video .title );
50- System .out .println ("Tags: " + video .tags );
51- System .out .println ("URL: " + video .url );
52- }
53- }
46+ Type type = new TypeToken <List <User >>() {}.getType ();
47+ List <User > users = (List <User >)request .execute ().parseAs (type );
48+ System .out .println (users );
49+ url .appendRawPath ("/eugenp" );
50+ request = requestFactory .buildGetRequest (url );
51+ ExecutorService executor = Executors .newSingleThreadExecutor ();
52+ Future <HttpResponse > responseFuture = request .executeAsync (executor );
53+ User eugen = responseFuture .get ().parseAs (User .class );
54+ System .out .println (eugen );
55+ executor .shutdown ();
5456 }
5557
5658 public static void main (String [] args ) {
5759 try {
58- try {
59- run ();
60- return ;
61- } catch (HttpResponseException e ) {
62- System .err .println (e .getMessage ());
63- }
64- } catch (Throwable t ) {
65- t .printStackTrace ();
60+ run ();
61+ } catch (Exception e ) {
62+ System .err .println (e .getMessage ());
6663 }
67- System .exit (1 );
6864 }
69-
7065}
0 commit comments