Created
June 10, 2017 19:22
-
-
Save rikoudosenin/1339667dafaf67fc45208d210217126f to your computer and use it in GitHub Desktop.
okhttp
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 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