Compare commits
2 Commits
ccaf6dbe62
...
0043e7f1bd
Author | SHA1 | Date | |
---|---|---|---|
0043e7f1bd | |||
6d9e7c5929 |
27
hellofx/app/src/main/java/client/CreateEventController.java
Normal file
27
hellofx/app/src/main/java/client/CreateEventController.java
Normal file
@ -0,0 +1,27 @@
|
||||
package client;
|
||||
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.Node;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
public class CreateEventController {
|
||||
|
||||
public CreateEventController(){}
|
||||
|
||||
@FXML
|
||||
public void initialize(){}
|
||||
|
||||
|
||||
@FXML
|
||||
protected void createBtnClick(ActionEvent event){
|
||||
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
|
||||
stage.close();
|
||||
}
|
||||
|
||||
@FXML
|
||||
protected void abortBtnClick(ActionEvent event){
|
||||
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
|
||||
stage.close();
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package client;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
@ -12,6 +13,7 @@ public class MainApplication extends Application {
|
||||
@Override
|
||||
public void start(Stage stage) throws IOException {
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("main-view.fxml"));
|
||||
|
||||
Scene scene = new Scene(fxmlLoader.load(), 1200, 700);
|
||||
scene.getStylesheets().add(Objects.requireNonNull(MainApplication.class.getResource("main-view.css")).toExternalForm());
|
||||
stage.setTitle("Hello!");
|
||||
|
@ -1,42 +1,61 @@
|
||||
package client;
|
||||
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Node;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.ScrollPane;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.stage.Modality;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.stage.StageStyle;
|
||||
import res.Event;
|
||||
import res.DataController;
|
||||
import res.Event;
|
||||
|
||||
import javafx.event.ActionEvent;
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
|
||||
public class MainController {
|
||||
|
||||
@FXML
|
||||
private GridPane calendarGrid;
|
||||
|
||||
@FXML
|
||||
private VBox vBoxMon;
|
||||
@FXML
|
||||
private VBox vBoxTue;
|
||||
@FXML
|
||||
private VBox vBoxWen;
|
||||
@FXML
|
||||
private VBox vBoxThu;
|
||||
@FXML
|
||||
private VBox vBoxFri;
|
||||
@FXML
|
||||
private VBox vBoxSat;
|
||||
@FXML
|
||||
private VBox vBoxSun;
|
||||
private Label LabelMonth;
|
||||
|
||||
private final String[] dayNames = {"Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"};
|
||||
private final Label[] dayLabel = new Label[7];
|
||||
private final VBox[] dayVBoxes = new VBox[7];
|
||||
|
||||
private int weekOffset;
|
||||
private LocalDateTime weekStartDateTime;
|
||||
|
||||
|
||||
public MainController() {
|
||||
weekOffset = 0;
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void initialize(){
|
||||
createWeek();
|
||||
setDates();
|
||||
|
||||
DataController dataController = new DataController();
|
||||
ArrayList<Event> eventList = dataController.getAllVisibleEvents();
|
||||
|
||||
for(Event event : eventList){
|
||||
addEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
protected void onAddBtnClick(){
|
||||
@ -49,34 +68,109 @@ public class MainController {
|
||||
stage.setScene(scene);
|
||||
stage.initModality(Modality.APPLICATION_MODAL);
|
||||
stage.setResizable(false);
|
||||
stage.initStyle(StageStyle.UNDECORATED);
|
||||
//stage.initStyle(StageStyle.UNDECORATED);
|
||||
stage.showAndWait();
|
||||
}
|
||||
catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
DataController dataController = new DataController();
|
||||
Event[] eventList = dataController.getAllEvents();
|
||||
|
||||
for(Event event : eventList){
|
||||
private void createWeek(){
|
||||
for (int i = 0; i < 7; i++) {
|
||||
Label label = new Label();
|
||||
label.setText(event.toString());
|
||||
label.setTextFill(Color.WHITE);
|
||||
vBoxWen.getChildren().add(label);
|
||||
label.setText(dayNames[i]);
|
||||
label.setMaxHeight(Double.MAX_VALUE);
|
||||
label.setMaxWidth(Double.MAX_VALUE);
|
||||
label.getStyleClass().add("labelDays");
|
||||
dayLabel[i] = label;
|
||||
calendarGrid.add(label, i, 0);
|
||||
|
||||
ScrollPane scrollPane = new ScrollPane();
|
||||
|
||||
VBox vBox = new VBox();
|
||||
vBox.getStyleClass().add("vBoxDays");
|
||||
vBox.setSpacing(10);
|
||||
dayVBoxes[i] = vBox;
|
||||
scrollPane.setContent(vBox);
|
||||
|
||||
scrollPane.setFitToWidth(true);
|
||||
scrollPane.setFitToHeight(true);
|
||||
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
|
||||
scrollPane.getStyleClass().add("scrollDays");
|
||||
|
||||
calendarGrid.add(scrollPane, i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
protected void createBtnClick(ActionEvent event){
|
||||
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
|
||||
stage.close();
|
||||
private void addEvent(Event event){
|
||||
VBox vBox = new VBox();
|
||||
vBox.getStyleClass().add("event");
|
||||
vBox.setSpacing(5);
|
||||
|
||||
HBox btnHBox = new HBox();
|
||||
btnHBox.setAlignment(Pos.BOTTOM_RIGHT);
|
||||
Button deleteBtn = new Button();
|
||||
deleteBtn.setText(" X ");
|
||||
deleteBtn.setOnAction(e -> {
|
||||
LocalDateTime eventDate = event.getDate();
|
||||
int day = (int)Duration.between(weekStartDateTime.toLocalDate().atStartOfDay(), eventDate.toLocalDate().atStartOfDay()).toDays();
|
||||
dayVBoxes[day].getChildren().remove(vBox);
|
||||
});
|
||||
Button editBtn = new Button();
|
||||
editBtn.setText("edit");
|
||||
btnHBox.getChildren().add(editBtn);
|
||||
btnHBox.getChildren().add(deleteBtn);
|
||||
vBox.getChildren().add(btnHBox);
|
||||
|
||||
Label nameLabel = new Label(event.getName());
|
||||
vBox.getChildren().add(nameLabel);
|
||||
|
||||
Label timeLabel = new Label(event.getStart() + "-" + event.getEnd());
|
||||
vBox.getChildren().add(timeLabel);
|
||||
|
||||
Label typeLabel = new Label("Wer: " + event.getOwnerName());
|
||||
vBox.getChildren().add(typeLabel);
|
||||
|
||||
/*
|
||||
Ä, ä \u00c4, \u00e4
|
||||
Ö, ö \u00d6, \u00f6
|
||||
Ü, ü \u00dc, \u00fc
|
||||
ß \u00df
|
||||
*/
|
||||
Label prioLabel = new Label("Priorit\u00e4t: " + event.getPriority());
|
||||
vBox.getChildren().add(prioLabel);
|
||||
|
||||
if(event.isFullDay()){
|
||||
Label fullDayLabel = new Label("Dieser Termin bockiert den ganzen Tag!");
|
||||
vBox.getChildren().add(fullDayLabel);
|
||||
}
|
||||
|
||||
|
||||
LocalDateTime eventDate = event.getDate();
|
||||
|
||||
int day = (int)Duration.between(weekStartDateTime.toLocalDate().atStartOfDay(), eventDate.toLocalDate().atStartOfDay()).toDays();
|
||||
|
||||
if(day >= 0 && day < 7){
|
||||
dayVBoxes[day].getChildren().add(vBox);
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
protected void abortBtnClick(ActionEvent event){
|
||||
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
|
||||
stage.close();
|
||||
private void setDates(){
|
||||
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("LLLL yyyy");
|
||||
DateTimeFormatter dayFormatter = DateTimeFormatter.ofPattern("E dd.MM");
|
||||
DateTimeFormatter dayOfWeekFormatter = DateTimeFormatter.ofPattern("e");
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
int dayOfWeek = Integer.parseInt(dayOfWeekFormatter.format(now));
|
||||
|
||||
weekStartDateTime = now.minusDays(weekOffset * 7L + dayOfWeek - 1);
|
||||
|
||||
for (int i = 0; i < 7; i++) {
|
||||
dayLabel[i].setText(dayFormatter.format(weekStartDateTime.plusDays(i)));
|
||||
}
|
||||
|
||||
LabelMonth.setText(dateFormatter.format(weekStartDateTime));
|
||||
|
||||
}
|
||||
}
|
@ -3,8 +3,7 @@
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
|
||||
<?import com.sun.javafx.scene.control.DatePickerContent?>
|
||||
<GridPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.MainController">
|
||||
<GridPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.CreateEventController">
|
||||
|
||||
<columnConstraints>
|
||||
<ColumnConstraints/>
|
||||
|
@ -1,5 +1,14 @@
|
||||
* {
|
||||
-fx-base-background-color: #2B2D42;
|
||||
-fx-base1-background-color: #525E74;
|
||||
|
||||
-fx-main-border-color: #B0B0B0;
|
||||
-fx-main-text-color: #ffffff;
|
||||
}
|
||||
|
||||
|
||||
GridPane{
|
||||
-fx-background-color: #2B2D42;
|
||||
-fx-background-color: -fx-base-background-color;
|
||||
}
|
||||
|
||||
.main-panel{
|
||||
@ -13,11 +22,11 @@ GridPane{
|
||||
}
|
||||
|
||||
.gridCalendar Line{
|
||||
-fx-stroke: #B0B0B0;
|
||||
-fx-stroke: -fx-main-border-color;
|
||||
}
|
||||
|
||||
.gridCalender{
|
||||
-fx-background-color: #2B2D42;
|
||||
-fx-background-color: -fx-base-background-color;
|
||||
-fx-min-height: 80px;
|
||||
-fx-max-height: 80px;
|
||||
}
|
||||
@ -29,19 +38,33 @@ GridPane{
|
||||
-fx-font-weight: bold;
|
||||
}
|
||||
|
||||
.scrollDays{
|
||||
-fx-background-color: -fx-base-background-color;
|
||||
-fx-border-color: -fx-main-border-color;
|
||||
}
|
||||
|
||||
.vBoxDays{
|
||||
-fx-background-color: -fx-base-background-color;
|
||||
}
|
||||
|
||||
.labelDays{
|
||||
-fx-alignment: center;
|
||||
-fx-background-color: #525E74;
|
||||
-fx-background-color: -fx-base1-background-color;
|
||||
-fx-font-size: 25px;
|
||||
-fx-border-color: -fx-main-border-color;
|
||||
}
|
||||
|
||||
Label{
|
||||
-fx-text-fill: white;
|
||||
-fx-text-fill: -fx-main-text-color;
|
||||
}
|
||||
|
||||
.event{
|
||||
-fx-background-color: -fx-base1-background-color;
|
||||
}
|
||||
|
||||
.main-btn{
|
||||
-fx-background-color: #ffffff;
|
||||
-fx-text-fill: white;
|
||||
-fx-text-fill: -fx-main-text-color;
|
||||
-fx-background-radius: 5em;
|
||||
-fx-min-width: 40px;
|
||||
-fx-min-height: 40px;
|
||||
|
@ -36,7 +36,7 @@
|
||||
<RowConstraints />
|
||||
<RowConstraints vgrow="ALWAYS" />
|
||||
</rowConstraints>
|
||||
<Label maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" styleClass="labelMonth" textFill="WHITE" GridPane.columnIndex="0" GridPane.rowIndex="0">
|
||||
<Label fx:id="LabelMonth" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" styleClass="labelMonth" textFill="WHITE" GridPane.columnIndex="0" GridPane.rowIndex="0">
|
||||
Monat
|
||||
<padding>
|
||||
<Insets left="5" />
|
||||
@ -57,48 +57,7 @@
|
||||
<RowConstraints maxHeight="55.0" minHeight="25.0" vgrow="NEVER" />
|
||||
<RowConstraints percentHeight="-Infinity" vgrow="ALWAYS" />
|
||||
</rowConstraints>
|
||||
<Label maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" styleClass="labelDays" textFill="WHITE" GridPane.columnIndex="0" GridPane.rowIndex="0">
|
||||
Montag
|
||||
<padding>
|
||||
<Insets left="5" />
|
||||
</padding>
|
||||
</Label>
|
||||
<Label maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" styleClass="labelDays" textFill="WHITE" GridPane.columnIndex="1" GridPane.rowIndex="0">
|
||||
Dienstag
|
||||
<padding>
|
||||
<Insets left="5" />
|
||||
</padding>
|
||||
</Label>
|
||||
<Label maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" styleClass="labelDays" textFill="WHITE" GridPane.columnIndex="2" GridPane.rowIndex="0">
|
||||
Mittwoch
|
||||
<padding>
|
||||
<Insets left="5" />
|
||||
</padding>
|
||||
</Label>
|
||||
<Label maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" styleClass="labelDays" textFill="WHITE" GridPane.columnIndex="3" GridPane.rowIndex="0">
|
||||
Donnerstag
|
||||
<padding>
|
||||
<Insets left="5" />
|
||||
</padding>
|
||||
</Label>
|
||||
<Label maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" styleClass="labelDays" textFill="WHITE" GridPane.columnIndex="4" GridPane.rowIndex="0">
|
||||
Freitag
|
||||
<padding>
|
||||
<Insets left="5" />
|
||||
</padding>
|
||||
</Label>
|
||||
<Label maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" styleClass="labelDays" textFill="WHITE" GridPane.columnIndex="5" GridPane.rowIndex="0">
|
||||
Samstag
|
||||
<padding>
|
||||
<Insets left="5" />
|
||||
</padding>
|
||||
</Label>
|
||||
<Label maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" styleClass="labelDays" textFill="WHITE" GridPane.columnIndex="6" GridPane.rowIndex="0">
|
||||
Sonntag
|
||||
<padding>
|
||||
<Insets left="5" />
|
||||
</padding>
|
||||
</Label>
|
||||
|
||||
</GridPane>
|
||||
</GridPane>
|
||||
</GridPane>
|
||||
|
@ -4,47 +4,63 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
public class DataController {
|
||||
|
||||
private static final String ALL_EVENTS_ENDPOINT = "http://localhost:8080/vpr/all-events";
|
||||
private static final String ALL_USERS_ENDPOINT = "http://localhost:8080/vpr/all-users";
|
||||
|
||||
public Event[] getAllEvents(){
|
||||
private HttpRequest httpRequest;
|
||||
|
||||
public DataController(){
|
||||
httpRequest = new HttpRequest();
|
||||
}
|
||||
|
||||
|
||||
public ArrayList<Event> getAllVisibleEvents() {
|
||||
ArrayList<Event> eventList = new ArrayList<>();
|
||||
|
||||
try {
|
||||
String jsonResponse = httpRequest.sendPostRequest(ALL_EVENTS_ENDPOINT, "userId=1");
|
||||
System.out.println(jsonResponse);
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
//String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
||||
|
||||
for (Object obj : objectMapper.readValue(jsonResponse, Object[].class)){
|
||||
ArrayList<Object> list = new ArrayList<>();
|
||||
if (obj.getClass().isArray()) {
|
||||
list = (ArrayList<Object>) Arrays.asList((Object[])obj);
|
||||
} else if (obj instanceof Collection) {
|
||||
list = new ArrayList<>((Collection<?>) obj);
|
||||
}
|
||||
eventList.add(new Event(list));
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return eventList;
|
||||
}
|
||||
|
||||
public Event[] getAllEvents() {
|
||||
Event[] eventList = null;
|
||||
|
||||
try {
|
||||
URL url = new URL(ALL_EVENTS_ENDPOINT);
|
||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
|
||||
con.setConnectTimeout(5000);
|
||||
con.setReadTimeout(5000);
|
||||
|
||||
int status = con.getResponseCode();
|
||||
if(status == 200){
|
||||
BufferedReader in = new BufferedReader(
|
||||
new InputStreamReader(con.getInputStream()));
|
||||
String inputLine;
|
||||
StringBuilder content = new StringBuilder();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
content.append(inputLine);
|
||||
}
|
||||
in.close();
|
||||
|
||||
eventList = parseJsonToEventList(content.toString());
|
||||
for (Event e : eventList){
|
||||
System.out.println(e);
|
||||
}
|
||||
}else{
|
||||
System.out.println("Status: " + status);
|
||||
String jsonResponse = httpRequest.sendGetRequest("http://localhost:8080/vpr/all-events-test");
|
||||
eventList = parseJsonToEventList(jsonResponse);
|
||||
for (Event e : eventList) {
|
||||
System.out.println(e);
|
||||
}
|
||||
con.disconnect();
|
||||
}catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,11 @@
|
||||
package res;
|
||||
|
||||
import com.sun.jdi.event.StepEvent;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Event {
|
||||
|
||||
private int id;
|
||||
@ -9,6 +15,44 @@ public class Event {
|
||||
private String start;
|
||||
private String end;
|
||||
|
||||
private LocalDateTime date;
|
||||
|
||||
private int ownerId;
|
||||
private String ownerName;
|
||||
|
||||
/*
|
||||
Constructor for SELECT:
|
||||
e.id AS eid,
|
||||
e.name AS ename,
|
||||
e.start,
|
||||
e.end,
|
||||
e.priority,
|
||||
e.is_full_day,
|
||||
|
||||
ue.date,
|
||||
|
||||
u.id AS uid,
|
||||
u.forename,
|
||||
u.name AS uname
|
||||
*/
|
||||
|
||||
public Event(ArrayList<Object> arr) {
|
||||
id = (int)arr.get(0);
|
||||
name = (String)arr.get(1);
|
||||
start = (String)arr.get(2);
|
||||
end = (String)arr.get(3);
|
||||
priority = (int)arr.get(4);
|
||||
isFullDay = (Boolean)arr.get(5); //((String)arr.get(5)).equals("true");
|
||||
|
||||
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
|
||||
date = LocalDateTime.parse(arr.get(6) + " 00:00", formatter);
|
||||
|
||||
ownerId = (int)arr.get(7);
|
||||
ownerName = arr.get(8) + " " + arr.get(9);
|
||||
}
|
||||
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
@ -57,12 +101,36 @@ public class Event {
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
public LocalDateTime getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(LocalDateTime date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public int getOwnerId() {
|
||||
return ownerId;
|
||||
}
|
||||
|
||||
public void setOwnerId(int ownerId) {
|
||||
this.ownerId = ownerId;
|
||||
}
|
||||
|
||||
public String getOwnerName() {
|
||||
return ownerName;
|
||||
}
|
||||
|
||||
public void setOwnerName(String ownerName) {
|
||||
this.ownerName = ownerName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name +
|
||||
"\nVon: " + start +
|
||||
"\nBis: " + start +
|
||||
"\nisFullDay = " + isFullDay;
|
||||
(isFullDay ? "\nDen ganzen Tag lang" : "");
|
||||
|
||||
}
|
||||
}
|
||||
|
76
hellofx/data/src/main/java/res/HttpRequest.java
Normal file
76
hellofx/data/src/main/java/res/HttpRequest.java
Normal file
@ -0,0 +1,76 @@
|
||||
package res;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class HttpRequest {
|
||||
public String sendPostRequest(String urlString, String urlParameters) throws Exception{
|
||||
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
|
||||
int postDataLength = postData.length;
|
||||
|
||||
URL url = new URL(urlString);
|
||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||
|
||||
con.setDoOutput(true);
|
||||
con.setInstanceFollowRedirects(false);
|
||||
con.setRequestMethod("POST");
|
||||
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
con.setRequestProperty("charset", "utf-8");
|
||||
con.setRequestProperty("Content-Length", Integer.toString(postDataLength));
|
||||
con.setUseCaches(false);
|
||||
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
|
||||
wr.write(postData);
|
||||
}
|
||||
|
||||
int status = con.getResponseCode();
|
||||
if (status == 200) {
|
||||
BufferedReader in = new BufferedReader(
|
||||
new InputStreamReader(con.getInputStream()));
|
||||
String inputLine;
|
||||
StringBuilder content = new StringBuilder();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
content.append(inputLine);
|
||||
}
|
||||
in.close();
|
||||
|
||||
con.disconnect();
|
||||
return content.toString();
|
||||
} else {
|
||||
con.disconnect();
|
||||
throw new Exception("Status: " + status);
|
||||
}
|
||||
}
|
||||
|
||||
public String sendGetRequest(String urlString) throws Exception{
|
||||
URL url = new URL(urlString);
|
||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
|
||||
con.setConnectTimeout(5000);
|
||||
con.setReadTimeout(5000);
|
||||
|
||||
int status = con.getResponseCode();
|
||||
if (status == 200) {
|
||||
BufferedReader in = new BufferedReader(
|
||||
new InputStreamReader(con.getInputStream()));
|
||||
String inputLine;
|
||||
StringBuilder content = new StringBuilder();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
content.append(inputLine);
|
||||
}
|
||||
in.close();
|
||||
|
||||
con.disconnect();
|
||||
return content.toString();
|
||||
|
||||
} else {
|
||||
con.disconnect();
|
||||
throw new Exception("Status: " + status);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user