Skip to content

Instantly share code, notes, and snippets.

@pfn
Last active December 28, 2015 04:19
Show Gist options
  • Save pfn/7442087 to your computer and use it in GitHub Desktop.
Save pfn/7442087 to your computer and use it in GitHub Desktop.
private static Future<String> requestJson(final String uri) {
return Future.create(new Callable<String>() {
public String call() throws IOException {
OkHttpClient client = new OkHttpClient();
HttpURLConnection conn = client.open(new URL(uri));
InputStreamReader in =
new InputStreamReader(conn.getInputStream(), "utf-8");
try {
return CharStreams.toString(in);
} finally {
in.close();
conn.disconnect();
}
}
});
}
public Future<Config> getConfig() {
return requestJson(CONFIG).map(new Function<String,Config>() {
public Config apply(String in) {
Gson gson = new Gson();
Config.Envelope envelope = gson.fromJson(in, Config.Envelope.class);
return envelope.result;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment