《電子技術(shù)應(yīng)用》
您所在的位置:首頁(yè) > 其他 > 設(shè)計(jì)應(yīng)用 > 基于Android端MVP模式和響應(yīng)式網(wǎng)絡(luò)框架的設(shè)計(jì)與實(shí)現(xiàn)
基于Android端MVP模式和響應(yīng)式網(wǎng)絡(luò)框架的設(shè)計(jì)與實(shí)現(xiàn)
2021年電子技術(shù)應(yīng)用第2期
李 想1,2,特日根1,2,3
1.長(zhǎng)光衛(wèi)星技術(shù)有限公司,吉林 長(zhǎng)春130000;2.吉林省衛(wèi)星遙感應(yīng)用技術(shù)重點(diǎn)實(shí)驗(yàn)室,吉林 長(zhǎng)春130000; 3.中國(guó)科學(xué)院長(zhǎng)春光學(xué)精密機(jī)械與物理研究所,吉林 長(zhǎng)春130000
摘要: MVC(Model-View-Controller)模式是Android應(yīng)用開(kāi)發(fā)的傳統(tǒng)方式(用Activity/Fragment表示Controller層,用XML文件表示View層),隨著項(xiàng)目的頁(yè)面增多,邏輯復(fù)雜度提升,將使Activity文件變得臃腫,代碼耦合度明顯提高,不利于項(xiàng)目后期的升級(jí)和維護(hù)。通過(guò)對(duì)傳統(tǒng)MVC開(kāi)發(fā)模式與主流MVP(Model-View-Presenter)開(kāi)發(fā)模式進(jìn)行研究和比較,發(fā)現(xiàn)MVP開(kāi)發(fā)模式能夠更好地解決上述問(wèn)題。同時(shí),官方HttpURLConnection類(lèi)對(duì)于HTTP網(wǎng)絡(luò)請(qǐng)求的效率無(wú)法滿(mǎn)足業(yè)務(wù)需求,而Retrofit2+OkHttp3+RxJava2的響應(yīng)式網(wǎng)絡(luò)請(qǐng)求框架具有更高的響應(yīng)效率。以《長(zhǎng)光衛(wèi)星云極視》項(xiàng)目為背景,研究并驗(yàn)證MVP模式和Retrofit2+OkHttp3+RxJava2的響應(yīng)式網(wǎng)絡(luò)請(qǐng)求框架結(jié)合的可行性。
中圖分類(lèi)號(hào): TN915
文獻(xiàn)標(biāo)識(shí)碼: A
DOI:10.16157/j.issn.0258-7998.200224
中文引用格式: 李想,特日根. 基于Android端MVP模式和響應(yīng)式網(wǎng)絡(luò)框架的設(shè)計(jì)與實(shí)現(xiàn)[J].電子技術(shù)應(yīng)用,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,F(xiàn)ine 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 引言

    在當(dāng)今社會(huì),移動(dòng)端因其便攜性、低功耗以及無(wú)線網(wǎng)的快速接入等優(yōu)勢(shì),使得人們與外部世界進(jìn)行網(wǎng)絡(luò)連接更加方便而舒適。正因如此,移動(dòng)端編程成為了當(dāng)下最熱門(mén)的計(jì)算機(jī)編程領(lǐng)域之一。2019年第二季度移動(dòng)端操作系統(tǒng)市場(chǎng)份額表明,Android系統(tǒng)占比77.14%,iOS系統(tǒng)占比22.83%,其余系統(tǒng)不及1%,由此可知 Android在當(dāng)今手機(jī)行業(yè)起著舉足輕重的作用。隨著每一款應(yīng)用承載的功能不斷增多,其代碼管理也變得更為復(fù)雜。對(duì)于Android應(yīng)用開(kāi)發(fā)來(lái)說(shuō),用Android Studio編譯器生成Android項(xiàng)目時(shí),其生成的XML文件和Activity文件已經(jīng)對(duì)應(yīng)傳統(tǒng)MVC(Model-View-Controller)架構(gòu)模式的View層和Controller層,同時(shí)XML文件不能實(shí)現(xiàn)全部布局功能,因此部分View層內(nèi)容需交付給Activity文件完成。Activity文件隨著頁(yè)面和業(yè)務(wù)邏輯的不斷增加也會(huì)不斷增大,代碼間耦合度明顯提高[1-3],將對(duì)項(xiàng)目的升級(jí)和維護(hù)帶來(lái)障礙。因此,對(duì)于大中型項(xiàng)目來(lái)說(shuō),MVC架構(gòu)并不可取。

    對(duì)于一個(gè)常規(guī)項(xiàng)目,其網(wǎng)絡(luò)請(qǐng)求必不可少,雖然官方提供了諸如HttpURLConnection類(lèi)等HTTP請(qǐng)求方式,但該類(lèi)在大量網(wǎng)絡(luò)請(qǐng)求時(shí),其性能較差。

    針對(duì)此問(wèn)題,MVP(Model-View-Presenter)+Retrofit+OkHttp+RxJava的架構(gòu)應(yīng)運(yùn)而生,該架構(gòu)能有效降低代碼耦合度,使Activity文件的內(nèi)容更加單一,網(wǎng)絡(luò)請(qǐng)求和布局更新更加高效。對(duì)于整個(gè)項(xiàng)目而言,整體結(jié)構(gòu)更加清晰,代碼可維護(hù)性也得到大幅度提升。

    本文通過(guò)對(duì)MVP+Retrofit+OkHttp+RxJava的研究分析,以《長(zhǎng)光衛(wèi)星云極視》項(xiàng)目的登錄模塊為應(yīng)用案例,設(shè)計(jì)并驗(yàn)證Android應(yīng)用開(kāi)發(fā)中MVP模式和Retrofit2+OkHttp3+RxJava2的響應(yīng)式網(wǎng)絡(luò)請(qǐng)求框架結(jié)合的方法及可行性。




本文詳細(xì)內(nèi)容請(qǐng)下載:http://ihrv.cn/resource/share/2000003384




作者信息:

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

(1.長(zhǎng)光衛(wèi)星技術(shù)有限公司,吉林 長(zhǎng)春130000;2.吉林省衛(wèi)星遙感應(yīng)用技術(shù)重點(diǎn)實(shí)驗(yàn)室,吉林 長(zhǎng)春130000;

3.中國(guó)科學(xué)院長(zhǎng)春光學(xué)精密機(jī)械與物理研究所,吉林 長(zhǎng)春130000)

此內(nèi)容為AET網(wǎng)站原創(chuàng),未經(jīng)授權(quán)禁止轉(zhuǎn)載。