项目5 时 事 阅 读 本项目通过鸿蒙系统开发工具DevEco Studio,基于Java开发一款阅读类App,实现获取随机笑话、今日新闻、历史上的今天。 5.1总体设计 本部分包括系统架构和系统流程。 5.1.1系统架构 系统架构如图51所示。 图51系统架构 5.1.2系统流程 系统流程如图52所示。 图52系统流程 图53应用目录结构 5.2开发工具 本项目使用DevEco Studio开发工具,安装过程如下。 (1) 注册开发者账号,完成注册并登录,在官网下载DevEco Studio并安装。 (2) 模板类型选择Empty Feature Ability,设备类型选择Phone,语言类型选择Java,单击Next后填写相关信息。 (3) 创建后的应用目录结构如图53所示。 (4) 在src/main/java目录下进行阅读App的应用开发。 5.3开发实现 本部分包括界面设计和程序开发,下面分别给出各模块的功能介绍及相关代码。 5.3.1界面设计 本部分包括图片导入、界面布局和完整代码。 图54图片导入 1. 图片导入 首先,将选好的界面图片导入project文件; 然后,将picture(.jpg格式)保存在main/resources/base/media文件夹下,如图54所示。 2. 界面布局 本部分包括主界面、今日新闻和历史上的今天。 (1) 主界面。 <?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="top" ohos:orientation="vertical" ohos:background_element="$graphic:background_ability_main" > //App名称显示在主界面中 <Text ohos:height="80vp" ohos:width="match_parent" ohos:text="悦读" ohos:text_size="60vp" ohos:text_font="sans-serif-condensed-medium" ohos:bottom_margin="20vp" ohos:top_margin="0fp" /> (2) 今日新闻。 <?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> //ListContainer容器的设置 <ListContainer ohos:id="$+id:list1" ohos:height="match_parent" ohos:width="match_parent" ohos:orientation="vertical" /> </DirectionalLayout> (3) 历史上的今天。 <?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> //ListContainer容器的设置 <ListContainer ohos:id="$+id:list" ohos:height="match_parent" ohos:width="match_parent" ohos:orientation="vertical" /> </DirectionalLayout> 文件6 3. 完整代码 界面设计完整代码请扫描二维码文件6获取。 5.3.2程序开发 本部分包括打开URL接口、Json数据解析、HTML解析、主界面、今日新闻界面、历史上的今天界面和ListContainer适配器。 1. 打开URL接口 打开URL接口以便后续进行URL调用。 public class APIRequest { static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG"); public static String URLRequest(String myurl,String Reqmethod){ NetManager netManager = NetManager.getInstance(null); if (!netManager.hasDefaultNet()) { return ""; } //可以获取网络状态的变化 NetHandle netHandle = netManager.getDefaultNet(); HttpURLConnection connection = null; String resText=" "; StringBuilder response=new StringBuilder(); try { URL url = new URL(myurl); URLConnection urlConnection = netHandle.openConnection(url, java.net.Proxy.NO_PROXY); if (urlConnection instanceof HttpURLConnection) { connection = (HttpURLConnection) urlConnection; } connection.setRequestMethod(Reqmethod); connection.connect(); //可进行URL的其他操作 InputStream in = connection.getInputStream(); BufferedReader reader=new BufferedReader(new InputStreamReader(in)); String line; while((line= reader.readLine())!=null){ response.append(line); } HiLog.info(LABEL,response.toString()); } catch(IOException e) { e.printStackTrace(); } finally { if (connection != null){ connection.disconnect(); } } resText=response.toString(); return resText; } } 文件7 2. Json数据解析 使用Gson解析Json数据,包括简单数组数据和复杂的多个数组,相关代码请扫描二维码文件7获取。 3. HTML解析 HTML解析去除界面的HTML标签。 public class HTMLChange { public static String delHTMLTag(String htmlStr) { String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式 String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式 Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE); Matcher m_script = p_script.matcher(htmlStr); htmlStr = m_script.replaceAll(""); //过滤script标签 Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE); Matcher m_style = p_style.matcher(htmlStr); htmlStr = m_style.replaceAll(""); //过滤style标签 Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE); Matcher m_html = p_html.matcher(htmlStr); htmlStr = m_html.replaceAll(""); //过滤HTML标签 return htmlStr.trim(); //返回文本字符串 } //删除标签 public static String stripHtml(String content) { //<p>段落替换为换行 content = content.replaceAll("<p .*?>", "\r\n"); //<br><br/>替换为换行 content = content.replaceAll("<br\\s*/?>", "\r\n"); //去掉其他<>之间的内容 content = content.replaceAll("\\<.*?>", ""); //还原HTML //content = HTMLDecoder.decode(content); return content; } } 文件8 4. 主界面 调用上述三种方法进行线程管理及线程间的通信,相关代码请扫描二维码文件8获取。 5. 今日新闻界面 public class NewsTodayAbilitySlice extends AbilitySlice { private ListContainer list1; private List<JsonParse.NewsToday> newsTodays; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_news_today); String data = intent.getStringParam("newsToday"); //getStringParam方法获取界面传递的参数 newsTodays = JsonParse.parseNewsToday(data); list1 = findComponentById(ResourceTable.Id_list1); //对应XML界面中的参数 initListContainer(); } //适配器代码从官方文档中获取 private void initListContainer() { SampleItemProvider1 sampleItemProvider1 = new SampleItemProvider1(newsTodays, this); list1.setItemProvider(sampleItemProvider1); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } } 6. 历史上的今天界面 public class HistoryTodayAbilitySlice extends AbilitySlice { private ListContainer list; private List<JsonParse.HistoryToday> historyTodays; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_history_today); String data = intent.getStringParam("historyToday"); //getStringParam方法获取界面传递的参数 historyTodays = JsonParse.parseHistoryToday(data); list = findComponentById(ResourceTable.Id_list); initListContainer(); } //适配器代码从官方文档中获取 private void initListContainer() { SampleItemProvider sampleItemProvider = new SampleItemProvider(historyTodays, this); list.setItemProvider(sampleItemProvider); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } } 文件9 7. ListContainer适配器 ListContainer每行可以是不同的数据,因此需要适配不同的数据结构,使其都能添加到ListContainer上,相关代码请扫描二维码文件9获取。 5.4成果展示 打开App,应用初始界面如图55所示; 今日新闻功能如图56所示; 历史上的今天功能如图57所示; 随机笑话功能如图58所示。 图55应用初始界面 图56今日新闻功能 图57历史上的今天功能 图58随机笑话功能