package zacksolutions.base; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; /** * JokesAPI */ public class JokesAPI { private String setup; private String punchline; public void fetchJokes() throws IOException { URL apiUrl = new URL("https://official-joke-api.appspot.com/jokes/random"); HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection(); connection.setRequestProperty("accept", "application/json"); InputStream responseStream = connection.getInputStream(); ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(responseStream); this.setup = root.path("setup").asText(); this.punchline = root.path("punchline").asText(); } public String getSetup(){ return setup; } public String getPunchline(){ return punchline; } }