Skip to content

Instantly share code, notes, and snippets.

@talfco
Created June 18, 2017 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talfco/4274a70c2aed0c32fad3f1ba163cef54 to your computer and use it in GitHub Desktop.
Save talfco/4274a70c2aed0c32fad3f1ba163cef54 to your computer and use it in GitHub Desktop.
Example to fetch data from the football from the api.football-data.org API
package net.cloudburo.football;
import org.apache.http.*;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
/**
A simple Java REST GET example using the Apache HTTP library.
This executes a call against the api.football-data.org service and
returns a JSON result
Apache HttpClient: http://hc.apache.org/httpclient-3.x/
**/
public class FootballTestLession1 {
public static void main(String[] args) {
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
//http://api.football-data.org/v1/competitions/?season=2015
try {
// specify the host, protocol, and port
HttpHost target = new HttpHost("api.football-data.org", 80, "http");
// specify the get reqest
HttpGet getRequest = new HttpGet("/v1/competitions/?season=2015");
System.out.println("executing request to " + target);
HttpResponse httpResponse = httpclient.execute(target, getRequest);
HttpEntity entity = httpResponse.getEntity();
System.out.println("----------------------------------------");
System.out.println(httpResponse.getStatusLine());
Header[] headers = httpResponse.getAllHeaders();
for (int i = 0; i < headers.length; i++) {
System.out.println(headers[i]);
}
System.out.println("----------------------------------------");
if (entity != null) {
System.out.println(EntityUtils.toString(entity));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
try {
httpclient.close();
} catch (Exception e) {}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment