Retrofit2教程,適合初學者(請求道數據解析注意)

RetrofitService類

一、public interfaceRetrofitService {

//請求方式為GET,參數為basil2style,因為沒有變量所以下面getString方法也不需要參數

@GET("txt/car.json")

//定義返回的方法,返回的響應體使用了ResponseBody

CallgetString();

}

二、GsonUtils類封裝

**

* Gson工具類

* 1,把json轉換成Object

* 2,把Object轉換成json

* 3,該方法主要功能是將json字符串轉換成指定類型的對象

*/

public final classGsonUtils {

/**

* 工具類對象

* (單例)

*/

private static finalGsonUtilsgsonUtils=newGsonUtils();

/**

* Gson對象

*/

private static finalGsongson=newGson();

/**

* 私有構造

*/

privateGsonUtils() {

}

/**

* 對外提供靜態公有的方法

*

*@return本類對象

*/

public staticGsonUtilsgetInstance() {

returngsonUtils;

}

/**

* 1,該方法主要功能是將json字符串轉換成Java類對象

*

*@paramjson

*? ? ? ? ? ? json字符串

*@paramcls

*? ? ? ? ? ? Java類的字節碼對象

*@return解析后的Java類對象

*@throwsException

*? ? ? ? ? ? 如果解析中出了問題,或者是json不完整都會拋出這個異常信息

*/

publicTj2O(String json,Class cls)throwsException {

returngson.fromJson(json,cls);

}

/**

* 2,該方法主要功能是將Java類對象轉換成json字符串

*

*@paramobj

*? ? ? ? ? ? Java對象

*@returnjson字符串

*/

publicStringo2J(Object obj) {

returngson.toJson(obj);

}

/**

* 3,該方法主要功能是將json字符串轉換成指定類型的對象

*

*@paramjson

*? ? ? ? ? ? json字符串

*@paramtypeOfT

*? ? ? ? ? ? 指定類型

*@return指定類型的對象

*/

publicTj2T(String json,Type typeOfT)throwsException {

returngson.fromJson(json,typeOfT);

}

}

三、RetrofitAdapter

public classRetrofitAdapterextendsBaseAdapter {

Listdata;

ContextmCtx;

publicRetrofitAdapter(Context mCtx,List data) {

this.mCtx= mCtx;

this.data= data;

}

@Override

public intgetCount() {

if(data!=null) {

returndata.size();

}

return0;

}

@Override

publicObjectgetItem(intposition) {

returndata.get(position);

}

@Override

public longgetItemId(intposition) {

returnposition;

}

@Override

publicViewgetView(intposition,View convertView,ViewGroup viewGroup) {

ViewHolder holder;

if(convertView ==null) {

//把條目布局轉化為view對象

convertView = View.inflate(mCtx,R.layout.item_retrofit, null);

//初始化holder對象,并初始化holder中的控件

holder =newViewHolder(convertView);

convertView.setTag(holder);

}else{

//如果當前view存在,則直接從中取出其保存的控件及數據

holder = (ViewHolder) convertView.getTag();

}

//通過position獲取當前item的car數據,從car數據中取出title、pubDate和image

CarBean.DataBean car =data.get(position);

holder.llTvTitle.setText(car.getTitle());

holder.llTvTime.setText(car.getPubDate());

//使用SmartImageView的setImageUrl方法下載圖片

//? holder.ll_iv_async.setImageUrl(car.image);

Picasso.with(mCtx).load(car.getImage()).into(holder.llIvRetrofit);

returnconvertView;

}

classViewHolder {

@InjectView(R.id.ll_iv_retrofit)

ImageViewllIvRetrofit;

@InjectView(R.id.ll_tv_title)

TextViewllTvTitle;

@InjectView(R.id.ll_tv_time)

TextViewllTvTime;

ViewHolder(View view) {

ButterKnife.inject(this,view);

}

}

}

四、Retrofit2Activity

public classRetrofit2ActivityextendsActivity {

@InjectView(com.tjhq.mygson.R.id.lv_retrofit_gson)

ListViewlvRetrofitGson;

Listdata;

@Override

protected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(com.tjhq.mygson.R.layout.activity_retrofit2);

ButterKnife.inject(this);

initData();

}

private voidinitData() {

initRetrofit();

}

private voidinitRetrofit() {

//獲取Retrofit對象,設置地址

Retrofit retrofit =newRetrofit.Builder()

.baseUrl(Constant.BASEURL)

.addConverterFactory(ScalarsConverterFactory.create())

.addConverterFactory(GsonConverterFactory.create())

.build();

RetrofitService retrofitService = retrofit.create(RetrofitService.class);

retrofitService.getString().enqueue(newCallback() {

@Override

public voidonResponse(Call call,Response response) {

//注意這里是拿到的對象需要轉換,千萬不要 String s = response.body().toString();這樣是很嚴重錯誤。

//解析一定要把對象轉成json,初學者可能這地方有的會出錯

String result = GsonUtils.getInstance().o2J(response.body());

Log.e("TAG","獲取數據"+result);

Gson gson =newGson();

CarBean carBean = gson.fromJson(result,CarBean.class);

Log.e("TAG","===="+carBean.getPageNo()+"===="+carBean.getPageSize()+"===="+carBean.getTotal());

data=carBean.getData();

RetrofitAdapter retrofitAdapter =newRetrofitAdapter(Retrofit2Activity.this,data);

lvRetrofitGson.setAdapter(retrofitAdapter);

}

@Override

public voidonFailure(Call call,Throwable t) {

}

});

}}

五、CarBean 類


public class CarBean {

/**

* data : [{"articleType":"n","count":29,"downs":0,"firstImg":"http://img.pcauto.com.cn/images/pcautogallery/modle/article/20169/8/14733163729178590_600.jpg","id":"8562073","image":"http://img0.pcauto.com.cn/pcauto/1609/08/g_8562073_1473339813478_240x160.jpg","mtime":1473351348000,"pubDate":"2016-09-09","title":"新福特翼虎購車手冊 家用中配足夠實用","ups":26,"url":"http://www.pcauto.com.cn/teach/856/8562073.html"},{"articleType":"n","count":37,"downs":0,"firstImg":"http://img0.pcauto.com.cn/pcauto/1608/31/8655654_toutu_thumb.jpg","id":"8655654","image":"http://img0.pcauto.com.cn/pcauto/1609/02/g_8655654_1472800030976_240x160.jpg","mtime":1473351337000,"pubDate":"2016-09-09","title":"年輕人第一臺車 10萬左右精品車型推薦","ups":130,"url":"http://www.pcauto.com.cn/teach/865/8655654.html"},{"articleType":"n","count":35,"downs":0,"firstImg":"http://img0.pcauto.com.cn/pcauto/1609/06/8719572_toutu_thumb.jpg","id":"8719572","image":"http://img0.pcauto.com.cn/pcauto/1609/06/g_8719572_1473152785181_240x160.jpg","mtime":1473264982000,"pubDate":"2016-09-08","title":"豪門不\u201c壕\u201d 4款入門豪華SUV僅售23萬起","ups":143,"url":"http://www.pcauto.com.cn/teach/871/8719572.html"},{"articleType":"n","count":40,"downs":0,"firstImg":"http://img.pcauto.com.cn/images/pcautogallery/modle/article/20169/1/14727375445822660_600.jpg","id":"8705572","image":"http://img0.pcauto.com.cn/pcauto/1609/07/g_8705572_1473242245557_240x160.jpg","mtime":1473264969000,"pubDate":"2016-09-08","title":"明銳對比英朗 當歐洲紳士遇上美國大漢","ups":52,"url":"http://www.pcauto.com.cn/teach/870/8705572.html"},{"articleType":"n","count":68,"downs":0,"firstImg":"http://img.pcauto.com.cn/images/pcautogallery/modle/article/20169/6/14731526553913750_600.jpg","id":"8719262","image":"http://img0.pcauto.com.cn/pcauto/1609/06/g_8719262_1473151845818_240x160.jpg","mtime":1473153591000,"pubDate":"2016-09-06","title":"新晉英倫長軸距座駕 捷豹XFL實拍解析","ups":299,"url":"http://www.pcauto.com.cn/teach/871/8719262.html"},{"articleType":"n","count":100,"downs":0,"firstImg":"http://img0.pcauto.com.cn/pcauto/1609/07/8695292_999_thumb.jpg","id":"8695292","image":"http://img0.pcauto.com.cn/pcauto/1609/01/g_8695292_1472695974218_240x160.jpg","mtime":1473137438000,"pubDate":"2016-09-06","title":"15萬元搞定 四款獨立后懸掛合資SUV推薦","ups":117,"url":"http://www.pcauto.com.cn/teach/869/8695292.html"},{"articleType":"n","count":84,"downs":0,"firstImg":"http://img0.pcauto.com.cn/pcauto/1609/06/8718677_xin1000_thumb.jpg","id":"8718677","image":"http://img0.pcauto.com.cn/pcauto/1609/05/g_8718677_1473061488223_240x160.jpg","mtime":1473092132000,"pubDate":"2016-09-06","title":"8萬元選靠譜SUV 4款新推自主車型推薦","ups":91,"url":"http://www.pcauto.com.cn/teach/871/8718677.html"},{"articleType":"n","count":96,"downs":0,"firstImg":"http://img.pcauto.com.cn/images/pcautogallery/modle/article/20168/29/14724733055558460_600.jpg","id":"8683971","image":"http://img0.pcauto.com.cn/pcauto/1609/02/g_8683971_1472803720871_240x160.jpg","mtime":1473005791000,"pubDate":"2016-09-05","title":"凱美瑞對比雅閣 誰才是日系中級車霸主","ups":65,"url":"http://www.pcauto.com.cn/teach/868/8683971.html"},{"articleType":"n","count":136,"downs":0,"firstImg":"http://img0.pcauto.com.cn/pcauto/1609/04/8716791_00_thumb.jpg","id":"8716791","image":"http://img0.pcauto.com.cn/pcauto/1609/04/g_8716791_1473002216143_240x160.jpg","mtime":1473005746000,"pubDate":"2016-09-05","title":"精華都在這里 成都車展最值得關注的SUV","ups":390,"url":"http://www.pcauto.com.cn/teach/871/8716791.html"},{"articleType":"n","count":26,"downs":0,"firstImg":"http://img.pcauto.com.cn/images/pcautogallery/modle/article/20169/4/14729794978954170_600.jpg","id":"8716391","image":"http://img0.pcauto.com.cn/pcauto/1609/04/g_8716391_1472979896686_240x160.jpg","mtime":1472980188000,"pubDate":"2016-09-04","title":"2016成都車展:靜態評測奔馳新一代威霆","ups":312,"url":"http://www.pcauto.com.cn/teach/871/8716391.html"},{"articleType":"n","count":32,"downs":0,"firstImg":"http://img0.pcauto.com.cn/pcauto/1609/01/8700555_8207206_03_thumb.jpg","id":"8700555","image":"http://img0.pcauto.com.cn/pcauto/1609/01/g_8700555_1472716638381_240x160.jpg","mtime":1472919329000,"pubDate":"2016-09-04","title":"入門性價比爆炸 新款致炫購車手冊","ups":91,"url":"http://www.pcauto.com.cn/teach/870/8700555.html"},{"articleType":"n","count":70,"downs":0,"firstImg":"http://img.pcauto.com.cn/images/pcautogallery/modle/article/20169/2/14728310541595730_600.jpg","id":"8712133","image":"http://img0.pcauto.com.cn/pcauto/1609/02/g_8712133_1472831164431_240x160.jpg","mtime":1472832200000,"pubDate":"2016-09-03","title":"2016成都車展:靜態評測北京現代勝達","ups":468,"url":"http://www.pcauto.com.cn/teach/871/8712133.html"},{"articleType":"n","count":41,"downs":0,"firstImg":"http://img0.pcauto.com.cn/pcauto/1609/02/8710078_1000_thumb.jpg","id":"8710078","image":"http://img0.pcauto.com.cn/pcauto/1609/02/g_8710078_1472810381352_240x160.jpg","mtime":1472817162000,"pubDate":"2016-09-02","title":"2016成都車展:靜態評測新款瑪莎拉蒂總裁","ups":299,"url":"http://www.pcauto.com.cn/teach/871/8710078.html"},{"articleType":"n","count":62,"downs":0,"firstImg":"http://img.pcauto.com.cn/images/pcautogallery/modle/article/20169/2/14728116986128820_600.jpg","id":"8711094","image":"http://img0.pcauto.com.cn/pcauto/1609/02/g_8711094_1472812405190_240x160.jpg","mtime":1472812618000,"pubDate":"2016-09-02","title":"2016成都車展:靜態評測大眾新桑塔納","ups":1053,"url":"http://www.pcauto.com.cn/teach/871/8711094.html"},{"articleType":"n","count":28,"downs":0,"firstImg":"http://img.pcauto.com.cn/images/pcautogallery/modle/article/20169/2/14728073809221840_600.jpg","id":"8710334","image":"http://img0.pcauto.com.cn/pcauto/1609/02/g_8710334_1472807999865_240x160.jpg","mtime":1472808197000,"pubDate":"2016-09-02","title":"2016成都車展:靜態體驗北京現代悅納","ups":247,"url":"http://www.pcauto.com.cn/teach/871/8710334.html"},{"articleType":"n","count":31,"downs":0,"firstImg":"http://img.pcauto.com.cn/images/pcautogallery/modle/article/20169/2/14728054816668520_600.jpg","id":"8710116","image":"http://img0.pcauto.com.cn/pcauto/1609/02/g_8710116_1472805803455_240x160.jpg","mtime":1472806069000,"pubDate":"2016-09-02","title":"2016成都車展:靜態評測東南DX3","ups":247,"url":"http://www.pcauto.com.cn/teach/871/8710116.html"},{"articleType":"n","count":60,"downs":0,"firstImg":"http://img.pcauto.com.cn/images/pcautogallery/modle/article/20169/2/14728006933643890_600.jpg","id":"8709146","image":"http://img0.pcauto.com.cn/pcauto/1609/02/g_8709146_1472801055169_240x160.jpg","mtime":1472801551000,"pubDate":"2016-09-02","title":"2016成都車展:靜態評測寶馬X1混動版","ups":806,"url":"http://www.pcauto.com.cn/teach/870/8709146.html"},{"articleType":"n","count":87,"downs":0,"firstImg":"http://img.pcauto.com.cn/images/pcautogallery/modle/article/20169/2/14727918621883140_600.jpg","id":"8708181","image":"http://img0.pcauto.com.cn/pcauto/1609/02/g_8708181_1472793809972_240x160.jpg","mtime":1472794520000,"pubDate":"2016-09-02","title":"2016成都車展:靜態評測東風本田競瑞","ups":533,"url":"http://www.pcauto.com.cn/teach/870/8708181.html"},{"articleType":"n","count":34,"downs":0,"firstImg":"http://img0.pcauto.com.cn/pcauto/1609/02/8704693_toutu_thumb.jpg","id":"8704693","image":"http://img0.pcauto.com.cn/pcauto/1609/02/g_8704693_1472787714022_240x160.jpg","mtime":1472793542000,"pubDate":"2016-09-02","title":"沖擊市場有力競爭者 新科沃茲購車手冊","ups":117,"url":"http://www.pcauto.com.cn/teach/870/8704693.html"},{"articleType":"n","count":111,"downs":0,"firstImg":"http://img.pcauto.com.cn/images/pcautogallery/modle/article/20169/2/14727803654960920_600.jpg","id":"8706132","image":"http://img0.pcauto.com.cn/pcauto/1609/02/g_8706132_1472781925547_240x160.jpg","mtime":1472781940000,"pubDate":"2016-09-02","title":"7座對標漢蘭達 斯柯達KODIAQ實拍解析","ups":104,"url":"http://www.pcauto.com.cn/teach/870/8706132.html"}]

* pageNo : 1

* pageSize : 20

* total : 200

*/

private intpageNo;

private intpageSize;

private inttotal;

privateListdata;

public intgetPageNo() {

returnpageNo;

}

public voidsetPageNo(intpageNo) {

this.pageNo= pageNo;

}

public intgetPageSize() {

returnpageSize;

}

public voidsetPageSize(intpageSize) {

this.pageSize= pageSize;

}

public intgetTotal() {

returntotal;

}

public voidsetTotal(inttotal) {

this.total= total;

}

publicListgetData() {

returndata;

}

public voidsetData(List data) {

this.data= data;

}

public static classDataBean {

/**

* articleType : n

* count : 29

* downs : 0

* firstImg : http://img.pcauto.com.cn/images/pcautogallery/modle/article/20169/8/14733163729178590_600.jpg

* id : 8562073

* image : http://img0.pcauto.com.cn/pcauto/1609/08/g_8562073_1473339813478_240x160.jpg

* mtime : 1473351348000

* pubDate : 2016-09-09

* title : 新福特翼虎購車手冊 家用中配足夠實用

* ups : 26

* url : http://www.pcauto.com.cn/teach/856/8562073.html

*/

privateStringarticleType;

private intcount;

private intdowns;

privateStringfirstImg;

privateStringid;

privateStringimage;

private longmtime;

privateStringpubDate;

privateStringtitle;

private intups;

privateStringurl;

publicStringgetArticleType() {

returnarticleType;

}

public voidsetArticleType(String articleType) {

this.articleType= articleType;

}

public intgetCount() {

returncount;

}

public voidsetCount(intcount) {

this.count= count;

}

public intgetDowns() {

returndowns;

}

public voidsetDowns(intdowns) {

this.downs= downs;

}

publicStringgetFirstImg() {

returnfirstImg;

}

public voidsetFirstImg(String firstImg) {

this.firstImg= firstImg;

}

publicStringgetId() {

returnid;

}

public voidsetId(String id) {

this.id= id;

}

publicStringgetImage() {

returnimage;

}

public voidsetImage(String image) {

this.image= image;

}

public longgetMtime() {

returnmtime;

}

public voidsetMtime(longmtime) {

this.mtime= mtime;

}

publicStringgetPubDate() {

returnpubDate;

}

public voidsetPubDate(String pubDate) {

this.pubDate= pubDate;

}

publicStringgetTitle() {

returntitle;

}

public voidsetTitle(String title) {

this.title= title;

}

public intgetUps() {

returnups;

}

public voidsetUps(intups) {

this.ups= ups;

}

publicStringgetUrl() {

returnurl;

}

public voidsetUrl(String url) {

this.url= url;

}

}

}

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,786評論 6 534
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,656評論 3 419
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,697評論 0 379
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,098評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,855評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,254評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,322評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,473評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,014評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,833評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,016評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,568評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,273評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,680評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,946評論 1 288
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,730評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,006評論 2 374

推薦閱讀更多精彩內容