如何在Android使用OkHttpUtils實現(xiàn)二次封裝?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
員工經(jīng)過長期磨合與沉淀,具備了協(xié)作精神,得以通過團隊的力量開發(fā)出優(yōu)質(zhì)的產(chǎn)品。創(chuàng)新互聯(lián)堅持“專注、創(chuàng)新、易用”的產(chǎn)品理念,因為“專注所以專業(yè)、創(chuàng)新互聯(lián)網(wǎng)站所以易用所以簡單”。公司專注于為企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計、微信公眾號開發(fā)、電商網(wǎng)站開發(fā),小程序開發(fā),軟件按需定制設(shè)計等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。
OkHttpUtils為什么進行二次封裝?
1、減少代碼量
2、后期換網(wǎng)絡(luò)處理框架方便
二次封裝的實現(xiàn)原理
1、將網(wǎng)絡(luò)請求提取在一個方法中
2、對里面的可變參數(shù),可以通過參數(shù)傳遞過去,也可以提供一個set方法傳遞過去
3、對于請求失敗和成功,我們可以使用接口回調(diào),讓調(diào)用該方法的對象處理
封裝后的網(wǎng)絡(luò)處理類的功能
1、網(wǎng)絡(luò)請求
2、xml數(shù)據(jù)轉(zhuǎn)換成javaBean
每一個處理網(wǎng)絡(luò)請求的ListView都要處理的3數(shù)據(jù)方法
1、初始化數(shù)據(jù)
2、下拉刷新數(shù)據(jù)
3、上拉加載數(shù)據(jù)
封裝前的代碼
/** * 3,加載更多 * 注意事項: * 請求成功數(shù)據(jù)更新之后,要關(guān)閉SpringView */ private void onDealLoadmore() { //資訊的網(wǎng)絡(luò)請求地址 String newsUrl = Constant.NEWS_URL; //http://www.oschina.net/action/api/news_list?pageIndex=0&catalog=1&pageSize=20 //關(guān)閉SpringView mSpringView.onFinishFreshAndLoad(); //網(wǎng)絡(luò)請求 OkHttpUtils .get() .url(newsUrl) .addParams("pageIndex", mCurrentPageIndex + "")//固定 .addParams("catalog", "1")//固定,1代表資訊 .addParams("pageSize", "20")//因為,一頁加載20條數(shù)據(jù) .build() .execute(new StringCallback() { @Override public void onError(Call call, Exception e, int id) { Toast.makeText(mContext, "上拉加載失敗", Toast.LENGTH_SHORT).show(); /* //關(guān)閉SpringView mSpringView.onFinishFreshAndLoad();*/ } @Override public void onResponse(String response, int id) { //請求成功,將字符串轉(zhuǎn)為javaBean,并獲取里面的泛型為News的集合 NewsList newsList = XmlUtils.toBean(NewsList.class, response.getBytes()); //對請求的數(shù)據(jù)進行非空判斷 if (newsList != null) { List<News> list = newsList.getList(); if (list != null && list.size() > 0) { //數(shù)據(jù)的更新 mData.addAll(newsList.getList()); //適配器的更新 mMyNewsPagerAdapter.notifyDataSetChanged(); //請求頁的索引要加1 ++mCurrentPageIndex; /* //關(guān)閉SpringView mSpringView.onFinishFreshAndLoad();*/ } } } }); }
封裝后的代碼
/** * 3,加載更多 * 注意事項: * 請求成功數(shù)據(jù)更新之后,要關(guān)閉SpringView */ private void onDealLoadmore() { mSpringView.onFinishFreshAndLoad(); mNewsPagerProtocol.setCurrentPageIndex(mCurrentPageIndex); mNewsPagerProtocol.loadData(new NewsPagerProtocol.Callback() { @Override public void onError(Call call, Exception e, int id) { Toast.makeText(mContext, "下拉刷新失敗", Toast.LENGTH_SHORT).show(); } @Override public void onResponse(NewsList newsList, int id) { if (newsList != null) { //獲取刷新的數(shù)據(jù)集合 List<News> list = newsList.getList(); //健壯性判斷 if (list != null && list.size() > 0) { //更新數(shù)據(jù)集合 mData.addAll(list); //更新適配器 mMyNewsPagerAdapter.notifyDataSetChanged(); //更新頁數(shù)的索引值 mCurrentPageIndex ++ ; } } } }); }
網(wǎng)絡(luò)封裝的代碼
/** * Author: 歸零 * Date: 2017/3/4 1:08 * Email: 4994766@qq.com * Description:網(wǎng)絡(luò)請求和數(shù)據(jù)解析 */ public class NewsPagerProtocol { private int mCurrentPageIndex; public void setCurrentPageIndex(int currentPageIndex) { mCurrentPageIndex = currentPageIndex; } public void loadData(final Callback callback) { //資訊的網(wǎng)絡(luò)請求地址 String newsUrl = Constant.NEWS_URL; //http://www.oschina.net/action/api/news_list?pageIndex=0&catalog=1&pageSize=20 //網(wǎng)絡(luò)請求 OkHttpUtils .get() .url(newsUrl) .addParams("pageIndex", mCurrentPageIndex + "")//固定 .addParams("catalog", "1")//固定,1代表資訊 .addParams("pageSize", "20")//因為,一頁加載20條數(shù)據(jù) .build() .execute(new StringCallback() { @Override public void onError(Call call, Exception e, int id) { //因為返回失敗處理的請求不一樣,所以不處理,交給調(diào)用的方法處理 callback.onError(call, e, id); } @Override public void onResponse(String response, int id) { //請求成功,將字符串轉(zhuǎn)為javaBean,并獲取里面的泛型為News的集合 NewsList newsList = XmlUtils.toBean(NewsList.class, response.getBytes()); //將轉(zhuǎn)換后的數(shù)據(jù)通過接口回調(diào),返回給調(diào)用方法的 callback.onResponse(newsList, id); } }); } public interface Callback { public void onError(Call call, Exception e, int id); public void onResponse(NewsList newsList, int id); } }
關(guān)于如何在Android使用OkHttpUtils實現(xiàn)二次封裝問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
文章標題:如何在Android使用OkHttpUtils實現(xiàn)二次封裝
轉(zhuǎn)載來于:http://muchs.cn/article38/ghsjsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、標簽優(yōu)化、外貿(mào)建站、App設(shè)計、小程序開發(fā)、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)