diff --git a/src/main/java/com/wudji/lessonlist/MainActivity.java b/src/main/java/com/wudji/lessonlist/MainActivity.java index 5632ceb..1810d12 100644 --- a/src/main/java/com/wudji/lessonlist/MainActivity.java +++ b/src/main/java/com/wudji/lessonlist/MainActivity.java @@ -9,8 +9,7 @@ import com.wudji.lessonlist.utils.ExceptionManager; import com.wudji.lessonlist.utils.FileControl; -import java.awt.Dimension; -import java.awt.Toolkit; +import java.awt.*; import java.util.Timer; import java.util.TimerTask; @@ -20,8 +19,8 @@ public class MainActivity { public static WindowConfig globalConfig = FileControl.getWindowConfig(); public static Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); public static String base_version = "v1.1.2"; - public static String product_version = "v1.2.4-poem-suggestion-tjyz"; - public static PoemScreen poemScreen; + public static String product_version = "v1.2.5-poem-suggestion-tjyz"; + public static PoemScreen poemScreen = new PoemScreen(new Point(1,1)); public static MainWindow window; public static WelcomeScreen welcomeScreen; public static NoticeScreen noticeScreen; @@ -42,25 +41,8 @@ public static void main(String[] args){ Timer timer = new Timer(); if (globalConfig.isEnablePoemSuggestion()){ - Thread thread = new Thread(() -> { - Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler()); - - poemScreen = new PoemScreen(window.getLocation()); - noticeScreen.updatePosition(poemScreen.getHeight()); - - timer.schedule(new TimerTask() { - @Override - public void run() { - refreshPoemInfo(poemScreen); - } - - private void refreshPoemInfo(PoemScreen poemScreen){ - - poemScreen.updatePoemInfo(); - } - }, 3600000,3600000); - }); - thread.start(); + poemScreen = new PoemScreen(window.getLocation()); + noticeScreen.updatePosition(poemScreen.getHeight()); } // 欢迎界面 @@ -95,6 +77,22 @@ private void updateA() { window.update(); } },1,1000); + + Thread thread = new Thread(() -> { + Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler()); + timer.schedule(new TimerTask() { + @Override + public void run() { + refreshPoemInfo(poemScreen); + } + + private void refreshPoemInfo(PoemScreen poemScreen){ + + poemScreen.updatePoemInfo(); + } + }, 3600000,3600000); + }); + thread.start(); } } class CustomExceptionHandler implements Thread.UncaughtExceptionHandler{ diff --git a/src/main/java/com/wudji/lessonlist/Screens/PoemScreen.java b/src/main/java/com/wudji/lessonlist/Screens/PoemScreen.java index e573bd7..a9c2d8a 100644 --- a/src/main/java/com/wudji/lessonlist/Screens/PoemScreen.java +++ b/src/main/java/com/wudji/lessonlist/Screens/PoemScreen.java @@ -77,14 +77,19 @@ public void updatePoemInfo(){ } // 标签行解析 - JSONArray suggestReasonArray = JSONObject.parseObject(poemDataString).getJSONObject("data").getJSONArray("matchTags"); - StringBuilder suggestReasonBuilder = new StringBuilder(); - for (Object suggestObj : suggestReasonArray) { - suggestReasonBuilder.append(suggestObj.toString()).append(" | "); + try{ + JSONArray suggestReasonArray = JSONObject.parseObject(poemDataString).getJSONObject("data").getJSONArray("matchTags"); + StringBuilder suggestReasonBuilder = new StringBuilder(); + for (Object suggestObj : suggestReasonArray) { + suggestReasonBuilder.append(suggestObj.toString()).append(" | "); + } + JLabel reasonLabel = new JLabel("诗词标签: " + suggestReasonBuilder.toString()); + reasonLabel.setFont(FileControl.getFont(Font.PLAIN,(int)(MainActivity.globalConfig.getNoticeFontSize() * 0.6))); + panel.add(reasonLabel); + } + catch (Exception ignored){ + } - JLabel reasonLabel = new JLabel("诗词标签: " + suggestReasonBuilder.toString()); - reasonLabel.setFont(FileControl.getFont(Font.PLAIN,(int)(MainActivity.globalConfig.getNoticeFontSize() * 0.6))); - panel.add(reasonLabel); // 添加信息按钮 JButton refreshButton = new JButton("🔎 诗词信息"); @@ -111,21 +116,29 @@ public void updatePoemInfo(){ private NoticeLine[] resultResolve(){ NoticeLine[] poemInfo = new NoticeLine[2]; - // 诗歌句子解析 - JSONObject fullData = JSONObject.parseObject(poemDataString); - // System.out.println(fullData.getString("status")); - if(Objects.equals(fullData.getString("status"), "success")){ - JSONObject poemData = fullData.getJSONObject("data"); - JSONObject originData = poemData.getJSONObject("origin"); - - poemInfo[0] = new NoticeLine(poemData.getString("content"),"bold",0,0,0,0); - poemInfo[1] = new NoticeLine("——" + originData.getString("author") + "《"+ originData.getString("title") +"》","italic",0,0,0,0); - - }else if(Objects.equals(fullData.getString("status"), "error")){ - poemInfo[0] = new NoticeLine("诗词获取失败,何故?","bold",0,0,0,0); - poemInfo[1] = new NoticeLine("——错误代码:" + fullData.getInteger("errCode").toString() + ";错误信息"+ fullData.getString("errMessage") +"。","italic",0,0,0,0); + try{ + + // 诗歌句子解析 + JSONObject fullData = JSONObject.parseObject(poemDataString); + // System.out.println(fullData.getString("status")); + if(Objects.equals(fullData.getString("status"), "success")){ + JSONObject poemData = fullData.getJSONObject("data"); + JSONObject originData = poemData.getJSONObject("origin"); + + poemInfo[0] = new NoticeLine(poemData.getString("content"),"bold",0,0,0,0); + poemInfo[1] = new NoticeLine("——" + originData.getString("author") + "《"+ originData.getString("title") +"》","italic",0,0,0,0); + + }else if(Objects.equals(fullData.getString("status"), "error")){ + poemInfo[0] = new NoticeLine("诗词获取失败,何故?","bold",0,0,0,0); + poemInfo[1] = new NoticeLine("——错误代码:" + fullData.getInteger("errCode").toString() + ";错误信息"+ fullData.getString("errMessage") +"。","italic",0,0,0,0); + } + return poemInfo; + } + catch (Exception ignored){ + poemInfo[0] = new NoticeLine("本次诗词获取失败","bold",0,0,0,0); + poemInfo[1] = new NoticeLine("请检查你的网络连接,或禁用每日诗词功能","italic",0,0,0,0); + return poemInfo; } - return poemInfo; } } diff --git a/src/main/java/com/wudji/lessonlist/network/PoemNetworkRequest.java b/src/main/java/com/wudji/lessonlist/network/PoemNetworkRequest.java index e278fae..26dd84f 100644 --- a/src/main/java/com/wudji/lessonlist/network/PoemNetworkRequest.java +++ b/src/main/java/com/wudji/lessonlist/network/PoemNetworkRequest.java @@ -59,11 +59,11 @@ private static String sendGetRequestWithHeader(String urlStr, String[]... header in.close(); } else { response.append("GET request failed. Response Code: ").append(responseCode); - ExceptionManager.showErrorDialog(new Throwable(response.toString())); + ExceptionManager.showErrorDialog(new Exception(response.toString())); } } catch (Exception e) { response.append("An error occurred: ").append(e.getMessage()); - ExceptionManager.showErrorDialog(new Throwable(response.toString())); + // ExceptionManager.showErrorDialog(new Exception(response.toString())); } return response.toString();