import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class HttpClient { public String getData(String urlStr) { String text = null; try { text = tryGetData(urlStr); } catch (IOException e) { System.err.println("Hiba! A lekérés sikertelen!"); System.err.println(e.getMessage()); } return text; } public String tryGetData(String urlStr) throws IOException { URL url = new URL(urlStr); HttpURLConnection http = (HttpURLConnection) url.openConnection(); http.setRequestMethod("GET"); http.connect(); String text = null; int responseCode = http.getResponseCode(); if (responseCode != 200) { throw new RuntimeException("Http válasz: " + responseCode); }else { text = new String( http.getInputStream().readAllBytes(), StandardCharsets.UTF_8); } return text; } public ArrayList getEmployees() { String urlStr = "http://[::1]:8000/employees"; String text = this.getData(urlStr); GsonBuilder builder = new GsonBuilder(); builder.setPrettyPrinting(); Gson gson = builder.create(); Employee[] employeeArray = gson.fromJson(text, Employee[].class); ArrayList list = new ArrayList<>(Arrays.asList(employeeArray)); return list; } }