《电子技术应用》
您所在的位置:首页 > 其他 > 设计应用 > 基于Android端MVP模式和响应式网络框架的设计与实现
基于Android端MVP模式和响应式网络框架的设计与实现
2021年电子技术应用第2期
李 想1,2,特日根1,2,3
1.长光卫星技术有限公司,吉林 长春130000;2.吉林省卫星遥感应用技术重点实验室,吉林 长春130000; 3.中国科学院长春光学精密机械与物理研究所,吉林 长春130000
摘要: MVC(Model-View-Controller)模式是Android应用开发的传统方式(用Activity/Fragment表示Controller层,用XML文件表示View层),随着项目的页面增多,逻辑复杂度提升,将使Activity文件变得臃肿,代码耦合度明显提高,不利于项目后期的升级和维护。通过对传统MVC开发模式与主流MVP(Model-View-Presenter)开发模式进行研究和比较,发现MVP开发模式能够更好地解决上述问题。同时,官方HttpURLConnection类对于HTTP网络请求的效率无法满足业务需求,而Retrofit2+OkHttp3+RxJava2的响应式网络请求框架具有更高的响应效率。以《长光卫星云极视》项目为背景,研究并验证MVP模式和Retrofit2+OkHttp3+RxJava2的响应式网络请求框架结合的可行性。
中图分类号: TN915
文献标识码: A
DOI:10.16157/j.issn.0258-7998.200224
中文引用格式: 李想,特日根. 基于Android端MVP模式和响应式网络框架的设计与实现[J].电子技术应用,2021,47(2):49-53,57.
英文引用格式: Li Xiang,Te Rigen. Design and implementation of Android-based MVP mode and responsive network request framework[J]. Application of Electronic Technique,2021,47(2):49-53,57.
Design and implementation of Android-based MVP mode and responsive network request framework
Li Xiang1,2,Te Rigen1,2,3
1.Chang Guang Satellite Technology Co.,Ltd.,Changchun 130000,China; 2.Main Laboratory of Satellite Remote Sensing Technology of Jilin Province,Changchun 130000,China; 3.Changchun Institute of Optics,Fine Mechanics and Physics,Chinese Academy of Sciences,Changchun 130000,China
Abstract: MVC(Model-View-Controller) mode is the traditional way of Android application development (Activity/Fragment is used to represent the Controller layer and XML files are used to represent the View layer). As the number of project pages increases, the logic complexity increases, and the Activity file becomes bloated and the code coupling is obvious. Improvement is not conducive to the upgrade and maintenance of the project later. By studying and comparing the traditional MVC development model and the mainstream MVP(Model-View-Presenter) development model, it is found that the MVP development model can better solve the above problems. At the same time, the efficiency of the official HttpURLConnection class for HTTP network requests can not meet business needs, and the responsive network request framework of Retrofit2+OkHttp3+RxJava2 has higher response efficiency. Based on the "Changguang Satellite Cloud Extreme Vision" project as the background, this paper studies and verifies the feasibility of combining the MVP mode with the responsive network request framework of Retrofit2+OkHttp3+RxJava2.
Key words : MVP mode;Retrofit;OkHttp;RxJava;responsive

0 引言

    在当今社会,移动端因其便携性、低功耗以及无线网的快速接入等优势,使得人们与外部世界进行网络连接更加方便而舒适。正因如此,移动端编程成为了当下最热门的计算机编程领域之一。2019年第二季度移动端操作系统市场份额表明,Android系统占比77.14%,iOS系统占比22.83%,其余系统不及1%,由此可知 Android在当今手机行业起着举足轻重的作用。随着每一款应用承载的功能不断增多,其代码管理也变得更为复杂。对于Android应用开发来说,用Android Studio编译器生成Android项目时,其生成的XML文件和Activity文件已经对应传统MVC(Model-View-Controller)架构模式的View层和Controller层,同时XML文件不能实现全部布局功能,因此部分View层内容需交付给Activity文件完成。Activity文件随着页面和业务逻辑的不断增加也会不断增大,代码间耦合度明显提高[1-3],将对项目的升级和维护带来障碍。因此,对于大中型项目来说,MVC架构并不可取。

    对于一个常规项目,其网络请求必不可少,虽然官方提供了诸如HttpURLConnection类等HTTP请求方式,但该类在大量网络请求时,其性能较差。

    针对此问题,MVP(Model-View-Presenter)+Retrofit+OkHttp+RxJava的架构应运而生,该架构能有效降低代码耦合度,使Activity文件的内容更加单一,网络请求和布局更新更加高效。对于整个项目而言,整体结构更加清晰,代码可维护性也得到大幅度提升。

    本文通过对MVP+Retrofit+OkHttp+RxJava的研究分析,以《长光卫星云极视》项目的登录模块为应用案例,设计并验证Android应用开发中MVP模式和Retrofit2+OkHttp3+RxJava2的响应式网络请求框架结合的方法及可行性。




本文详细内容请下载:http://www.chinaaet.com/resource/share/2000003384




作者信息:

李  想1,2,特日根1,2,3

(1.长光卫星技术有限公司,吉林 长春130000;2.吉林省卫星遥感应用技术重点实验室,吉林 长春130000;

3.中国科学院长春光学精密机械与物理研究所,吉林 长春130000)

此内容为AET网站原创,未经授权禁止转载。