После того, как детали курса найдены, объект Course создается из класса модели rest и сохраняется в списке.
Код: Выделить всё
public class Parser {
private static List courses;
public static void parse(File file) {
[collecting data...]
Course course = new Course(crn, subject, number, title, department, hours, instructor, size, seats, semester, term);
courses.add(course);
}
public static List getCourses() {
return courses;
}
}
Код: Выделить всё
/** Constructs a JSON request body and posts course data to the server. */
public static void saveCourses(List courses) throws URISyntaxException, IOException, InterruptedException {
for (int i = 0; i < courses.size(); i++) {
Gson gson = new Gson();
String json = gson.toJson(courses.get(i));
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest httpRequest = HttpRequest.newBuilder()
.uri(new URI("http://localhost:8080/course/save"))
.header("Content-Type", "application/json")
.POST(BodyPublishers.ofString(json))
.build();
httpClient.send(httpRequest, BodyHandlers.ofString());
}
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... t-requests
Мобильная версия