Skip to content

Instantly share code, notes, and snippets.

@cadams500
Last active December 18, 2015 08:49
Show Gist options
  • Save cadams500/5756962 to your computer and use it in GitHub Desktop.
Save cadams500/5756962 to your computer and use it in GitHub Desktop.
Example eDataSource API Usage
/*
* VERY raw way to read a restful API endpoint.
* This code can be refined and made into a very simple REST library quite easily.
*
* Code has not been tested and should be taken as pseudo-code.
*/
String apiKey = "myapi-key";
URL url = new URL("http://api.emaildatasource.com/service/2.0.0/rest/api.service.engagements?method=getAvailableDomains&api-key=" + apiKey);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
conn.connect();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
conn.disconnect();
Gson gson = new Gson();
String json = sb.toString();
String [] domains = gson.fromJson(json,String[].class);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment