Skip to content

Instantly share code, notes, and snippets.

@rikoudosenin
Created June 10, 2017 19:22
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 rikoudosenin/1339667dafaf67fc45208d210217126f to your computer and use it in GitHub Desktop.
Save rikoudosenin/1339667dafaf67fc45208d210217126f to your computer and use it in GitHub Desktop.
okhttp
package okhttpp;
import java.io.IOException;
import com.google.gson.Gson;
import json.TwitchAPI;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class Twitch {
private static OkHttpClient client = new OkHttpClient();
public static void main(String[] args) {
for(String str1 : getUserData("EUR"))
{
System.out.println(str1);
}
}
public static String getJSON(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public static String[] getUserData(String currency)
{
String json = null;
try {
json = getJSON("https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=" + currency);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Gson gson = new Gson();
TwitchAPI twitchapi = gson.fromJson(json, TwitchAPI.class);
return new String[] {
"Name: " + twitchapi.getName(),
"price in usd : " + twitchapi.getPrice_usd(),
"price in eur: " + twitchapi.getPrice_eur()
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment