Created
June 18, 2017 07:20
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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