/* * 正则获取当前 DataCount,未获取到值则返回原数值 */ private int getDataCount(String keyName, String urlString) throws JSONException, IOException { /* * 1. 定时器链接添加标志 platform=android&auto_timer=30&user_device_id=#{user_device_id} * 2. 读取本地缓存头文件 */ Map<String, String> headers = ApiHelper.checkResponseHeader(urlString, mAssetsPath); String extraParams = String.format( "platform=android&auto_timer=%d&user_device_id=%d", K.kTimerInterval, userJSON.getInt(K.kUserDeviceId)); String urlSplit = (urlString.contains("?") ? "&" : "?"); String urlStringWithExtraParams = String.format("%s%s%s", urlString, urlSplit, extraParams); Map<String, String> response = HttpUtil.httpGet(urlStringWithExtraParams, headers); String keyLastName = keyName + "_last"; if (!notificationJSON.has(keyName)) { notificationJSON.put(keyName, -1); } if (!notificationJSON.has(keyLastName)) { notificationJSON.put(keyLastName, -1); } int lastCount = notificationJSON.getInt(keyLastName); if (response.get(URLs.kCode).equals("200")) { /* * 1. 缓存头文件信息 * 2. 服务器响应信息写入本地 */ String htmlName = HttpUtil.UrlToFileName(urlString); String htmlPath = String.format("%s/%s", mAssetsPath, htmlName); String urlKey = urlString.contains("?") ? TextUtils.split(urlString, "?")[0] : urlString; ApiHelper.storeResponseHeader(urlKey, mAssetsPath, response); String htmlContent = response.get(URLs.kBody); htmlContent = htmlContent.replace( "/javascripts/", String.format("%s/javascripts/", mRelativeAssetsPath)); htmlContent = htmlContent.replace( "/stylesheets/", String.format("%s/stylesheets/", mRelativeAssetsPath)); htmlContent = htmlContent.replace("/images/", String.format("%s/images/", mRelativeAssetsPath)); FileUtil.writeFile(htmlPath, htmlContent); String strRegex = "\\bMobileBridge.setDashboardDataCount.+"; String countRegex = "\\d+"; Pattern patternString = Pattern.compile(strRegex); Pattern patternCount = Pattern.compile(countRegex); Matcher matcherString = patternString.matcher(htmlContent); matcherString.find(); String str = matcherString.group(); Matcher matcherCount = patternCount.matcher(str); if (matcherCount.find()) { int dataCount = Integer.parseInt(matcherCount.group()); /* * 如果tab_*_last 的值为 -1,表示第一次加载 */ if (lastCount == -1) { notificationJSON.put(keyLastName, dataCount); notificationJSON.put(keyName, 1); FileUtil.writeFile(notificationPath, notificationJSON.toString()); } return dataCount; } else { Log.i("notification", "未匹配到数值"); return lastCount; } } else if (response.get("code").equals("304")) { Log.i("notification", "当前无通知"); return lastCount; } else { Log.i("notification", "网络请求失败"); return lastCount; } }