美文网首页
Retrofit2教程,适合初学者(请求道数据解析注意)

Retrofit2教程,适合初学者(请求道数据解析注意)

作者: 张克同学 | 来源:发表于2017-12-18 17:47 被阅读0次

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;

}

}

}

相关文章

网友评论

      本文标题:Retrofit2教程,适合初学者(请求道数据解析注意)

      本文链接:https://www.haomeiwen.com/subject/zbbmwxtx.html