本文主要是介绍Retrofit2.0的简单使用 (包含Gson),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
http://blog.csdn.net/bskfnvjtlyzmv867/article/details/70667041 (转)
参考博客:
实现效果:
使用姿势:
1. 使用教程
- 添加依赖
- compile ‘com.squareup.retrofit2:retrofit:2.2.0’
- compile ‘com.squareup.retrofit2:converter-gson:2.2.0’
- AndroidStudio添加Gson插件——GsonFormat
2. 使用API
- 所用api:http://www.mrsorrow.xin/pro/rank_querySpecRankByUniv?university.univCode=10280&page=1
- 功能:根据高校代码进行学科专业类排名
-
返回json如下:
{"rows": [{"id": 1893,"specCode": "","specName": "冶金工程类","univCode": "10280","univName": "上海大学","univRank": "5","univScore": "78"},{"id": 328,"specCode": "0303","specName": "社会学类","univCode": "10280","univName": "上海大学","univRank": "8","univScore": "77"},{"id": 828,"specCode": "0503","specName": "新闻传播学类","univCode": "10280","univName": "上海大学","univRank": "10","univScore": "77"},{"id": 3959,"specCode": "1303","specName": "戏剧与影视学类","univCode": "10280","univName": "上海大学","univRank": "4","univScore": "77"},{"id": 396,"specCode": "0305","specName": "马克思主义理论类","univCode": "10280","univName": "上海大学","univRank": "17","univScore": "76"}]
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
3. 入门使用Retrofit
-
定义接口(封装URL地址和数据请求) :
@GET("rank_querySpecRankByUniv")
Call<ResponseBody> querySpecRankByUniv(@Query("university.univCode") String univCode,@Query("page") int page);
-
实例化Retrofit:
Retrofit retrofit = new Retrofit.Builder().baseUrl(Constant.BASE_URL).build();
-
通过Retrofit实例创建接口服务对象
QueryRankService service = retrofit.create(QueryRankService.class);
-
ResponseBody中存放着我们请求的数据,调用response.body().string()转化为字符串:
Call<ResponseBody> call = service.querySpecRankByUniv("10280", 1);call.enqueue(new Callback<ResponseBody>() {@Overridepublic void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {if(response.isSuccessful()){try {String result = response.body().string();mTextView.setText(result);} catch (IOException e) {e.printStackTrace();}}}@Overridepublic void onFailure(Call<ResponseBody> call, Throwable t) {}});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
5. 使用Gson解析
-
定义接口(封装URL地址和数据请求) :
@GET("rank_querySpecRankByUniv")Call<SpecRankBean> querySpecRankByUnivUseGson(@Query("university.univCode") String univCode,@Query("page") int page);
-
实例化Retrofit:
Retrofit retrofit = new Retrofit.Builder().baseUrl(Constant.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
-
将请求的数据可以直接转换为我们的Bean,通过各种get方法获取我们想要的数据:
call.enqueue(new Callback<SpecRankBean>() {@Overridepublic void onResponse(Call<SpecRankBean> call, Response<SpecRankBean> response) {if(response.isSuccessful()){SpecRankBean bean = response.body();List<SpecRankBean.RowsBean> list = bean.getRows();String result = "";for (SpecRankBean.RowsBean rowsBean : list) {result += rowsBean.getUnivName() + rowsBean.getSpecName() + "\n";}mTextView.setText(result);}}@Overridepublic void onFailure(Call<SpecRankBean> call, Throwable t) {}});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
这篇关于Retrofit2.0的简单使用 (包含Gson)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!