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阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容