Android基于OkHttpUtils網(wǎng)絡(luò)請求的二次封裝
OkHttpUtils網(wǎng)絡(luò)請求為什么進(jìn)行二次封裝?
1、減少代碼量
2、后期換網(wǎng)絡(luò)處理框架方便
二次封裝的實(shí)現(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,加載更多
* 注意事項(xiàng):
* 請求成功數(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")//因?yàn)?一頁加載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ù)進(jìn)行非空判斷
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,加載更多
* 注意事項(xiàng):
* 請求成功數(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")//因?yàn)?一頁加載20條數(shù)據(jù)
.build()
.execute(new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
//因?yàn)榉祷厥√幚淼恼埱蟛灰粯?所以不處理,交給調(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);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android View的事件分發(fā)機(jī)制深入分析講解
事件分發(fā)從手指觸摸屏幕開始,即產(chǎn)生了觸摸信息,被底層系統(tǒng)捕獲后會傳遞給Android的輸入系統(tǒng)服務(wù)IMS,通過Binder把消息發(fā)送到activity,activity會通過phoneWindow、DecorView最終發(fā)送給ViewGroup。這里就直接分析ViewGroup的事件分發(fā)2023-01-01
Eclipse開發(fā)環(huán)境導(dǎo)入android sdk的sample中的源碼
初學(xué)Android編程,Android SDK中提供的Sample代碼自然是最好的學(xué)習(xí)材料,需要的朋友可以參考下2012-12-12
Android自定義鍵盤的實(shí)現(xiàn)(數(shù)字鍵盤和字母鍵盤)
本篇文章主要介紹了Android自定義鍵盤的實(shí)現(xiàn)(數(shù)字鍵盤和字母鍵盤),具有一定的參考價值,有興趣的可以了解一下2017-08-08
Flutter自定義實(shí)現(xiàn)彈出層的示例代碼
這篇文章主要為大家詳細(xì)介紹了Flutter如何自定義組件實(shí)現(xiàn)彈出層的效果,?文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-08-08
android surfaceView實(shí)現(xiàn)播放視頻功能
這篇文章主要為大家詳細(xì)介紹了android surfaceView實(shí)現(xiàn)播放視頻功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05
Android 使用Vitamio打造自己的萬能播放器(7)——在線播放(下載視頻)
本文主要介紹Android Vitamio開發(fā)播放器,這里提供在線播放和下載視頻實(shí)例代碼,有需要的小伙伴可以參考下2016-07-07

