Skip to content

Commit

Permalink
修复链接校园网服务器超时或无网络连接导致的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
RedDragon0293 committed Sep 25, 2023
1 parent bd231d9 commit 05d9a6c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
37 changes: 36 additions & 1 deletion src/main/java/cn/reddragon/eportal/Authenticator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import cn.reddragon.eportal.utils.HttpUtils;

import java.awt.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.NoRouteToHostException;
import java.net.SocketTimeoutException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -77,7 +80,39 @@ public static boolean isOnline() {
online = false;
return false;
}
} catch (Exception e) {
} catch (SocketTimeoutException e) {
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().createImage("icon.png");
TrayIcon icon = new TrayIcon(image, "Error");
icon.setImageAutoSize(true);
try {
tray.add(icon);
} catch (AWTException e1) {
throw new RuntimeException(e1);
}
icon.displayMessage("Error", "Connection timeout!", TrayIcon.MessageType.ERROR);
tray.remove(icon);
System.exit(0);
}
return false;
} catch (NoRouteToHostException e) {
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().createImage("icon.png");
TrayIcon icon = new TrayIcon(image, "Error");
icon.setImageAutoSize(true);
try {
tray.add(icon);
} catch (AWTException e1) {
throw new RuntimeException(e1);
}
icon.displayMessage("Error", "No Internet connection!", TrayIcon.MessageType.ERROR);
tray.remove(icon);
System.exit(0);
}
return false;
} catch (IOException e) {
online = false;
return false;
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/cn/reddragon/eportal/Config.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cn.reddragon.eportal;

import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

Expand Down Expand Up @@ -47,7 +50,6 @@ public static String[] read() throws IOException {
}
String password = new String(b1);
stream.close();
System.out.println(username + " " + password);
return new String[]{username, password};
} catch (Exception e) {
e.printStackTrace();
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/cn/reddragon/eportal/HelloApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.IOException;
import java.awt.*;
import java.io.IOException;

public class HelloApplication extends Application {
private static Label statusLabel;
private static Button button;
public static final Thread askThread = new Thread(() -> {
while (true) {
try {
Thread.sleep(2500L);
} catch (InterruptedException e) {
//throw new RuntimeException(e);
}
Authenticator.isOnline();
Platform.runLater(() -> {
statusLabel.setText("Current status:" + (Authenticator.online ? "Online" : "Offline"));
Expand All @@ -29,11 +34,6 @@ public class HelloApplication extends Application {
button.setText("Login");
}
});
try {
Thread.sleep(2500L);
} catch (InterruptedException e) {
//throw new RuntimeException(e);
}
}
});

Expand Down Expand Up @@ -84,7 +84,6 @@ public static void main(String[] args) {
e.printStackTrace();
System.exit(0);
} catch (IOException ignored) {

}
launch();
System.exit(0);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/cn/reddragon/eportal/HelloController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import cn.reddragon.eportal.utils.HttpUtils;
import cn.reddragon.eportal.utils.IOUtils;
import cn.reddragon.eportal.utils.URIEncoder;
import com.google.gson.*;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
Expand All @@ -29,7 +30,7 @@ public class HelloController {

@FXML
protected void onLoginButtonClick() {
if (Authenticator.isOnline()) {
if (Authenticator.online) {
if (button.getText().equals("Login")) {
resultText.setText("Already logged in!");
button.setText("Logout");
Expand Down

0 comments on commit 05d9a6c

Please sign in to comment.