From 251c3b349c9b35909c7a65ab51e8689d9ea13925 Mon Sep 17 00:00:00 2001 From: Gray Zhang Date: Thu, 18 Sep 2025 23:25:32 +0800 Subject: [PATCH] refactor: improve node topic count parsing with better error handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Rename totalStr to totalCountRaw for better clarity - Add null/empty check before parsing - Use specific NumberFormatException instead of generic Exception - Add error logging for failed parsing attempts - Add descriptive comment explaining the field purpose This is a follow-up to PR #82, implementing improvements suggested by GitHub Copilot review. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../me/ghui/v2er/network/bean/NodeTopicInfo.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/me/ghui/v2er/network/bean/NodeTopicInfo.java b/app/src/main/java/me/ghui/v2er/network/bean/NodeTopicInfo.java index 21e29c41..f2c33e54 100644 --- a/app/src/main/java/me/ghui/v2er/network/bean/NodeTopicInfo.java +++ b/app/src/main/java/me/ghui/v2er/network/bean/NodeTopicInfo.java @@ -11,6 +11,7 @@ import me.ghui.v2er.network.Constants; import me.ghui.v2er.util.UriUtils; import me.ghui.v2er.util.Utils; +import me.ghui.v2er.util.L; /** * Created by ghui on 27/05/2017. @@ -21,7 +22,7 @@ public class NodeTopicInfo extends BaseInfo { @Pick("span.topic-count strong") - public String totalStr; + private String totalCountRaw; // Raw topic count string from HTML (may contain thousand separators) @Pick(value = "a[href*=favorite/] ", attr = Attrs.HREF) private String favoriteLink; @Pick("div.box div.cell:has(table)") @@ -32,9 +33,16 @@ public void setItems(List items) { } public int getTotal() { + if (totalCountRaw == null || totalCountRaw.isEmpty()) { + return 0; + } + try { - return Integer.parseInt(totalStr.replaceAll("[^0-9]", "")); - } catch (Exception e) { + // Remove thousand separators (commas) and any other non-numeric characters + String cleanedNumber = totalCountRaw.replaceAll("[^0-9]", ""); + return Integer.parseInt(cleanedNumber); + } catch (NumberFormatException e) { + L.e("Failed to parse topic count: " + totalCountRaw + ", error: " + e.getMessage()); return 0; } } @@ -70,7 +78,7 @@ public String getOnce() { public String toString() { return "NodeTopicInfo{" + "favoriteLink=" + favoriteLink + - ",total=" + totalStr + + ",total=" + totalCountRaw + ", items=" + items + '}'; }