[置顶] 浅谈我为什么选择用Retrofit作为我的网络请求框架
来源:互联网 发布:贵人出门多逢雨 知乎 编辑:程序博客网 时间:2024/06/05 10:46
版权声明:本文为博主原创文章,未经博主允许不得转载。
目录(?)[+]
- 比较AsyncTaskVolleyRetrofit三者的请求时间
- Retrofit20 完胜
- 使用
- 添加依赖
- 请求范例
- 声明接口
- 调用接口
- 进阶使用1ConverterFactory转换工厂
- Retrofit支持以下转换
- 添加依赖
- 定义java bean
- 接口方法声明
- 调用接口
- 进阶使用2 常用接口范例声明
比较AsyncTask、Volley、Retrofit三者的请求时间
Retrofit2.0 完胜
使用
添加依赖
build.gradle
compile ‘com.squareup.retrofit2:retrofit:2.0.0-beta4’
请求范例
以淘宝的ip库请求为例
- 地址:http://ip.taobao.com/service/getIpInfo.php
- 请求参数:ip
- 请求方法: get
声明接口
- public interface ApiControl {
- //@Query注解的作用理解为查询条件,这里表示需要查询的字段为ip
- //ResponseBody是Retrofit自带的返回类,
- @GET("http://ip.taobao.com/service/getIpInfo.php")
- Call<ResponseBody> getIpInfo(@Query("ip") String ip);
- }
调用接口
- //创建Retrofit实例
- Retrofit retrofit = new Retrofit.Builder()
- //当我们的@GET()里有url时,这个baseUrl无效。但是这个必须要填,不然会报错,神奇。
- .baseUrl("http://www.taobao.com.cn/")
- .build();
- ApiControl apiStores = retrofit.create(ApiControl.class);
- Call<ResponseBody> call = apiStores.getIpInfo("220.160.193.209");
- //在主线程里,异步调用。
- call.enqueue(new Callback<ResponseBody>() {
- @Override
- public void onResponse(Response<ResponseBody> response) {
- try {
- Log.i("onResponse", "response=" + response.body().string());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onFailure(Throwable t) {
- Log.i("onFailure", "onFailure=" + t.getMessage());
- }
- });
同步调用
- try {
- Response<ResponseBody> response = call.execute();
- } catch (IOException e) {
- e.printStackTrace();
- }
进阶使用1:ConverterFactory转换工厂
可以帮我们将获取到的数据转换为JavaBEAN
Retrofit支持以下转换
Gson: com.squareup.retrofit2:converter-gson
Jackson: com.squareup.retrofit2:converter-jackson
Moshi: com.squareup.retrofit2:converter-moshi
Protobuf: com.squareup.retrofit2:converter-protobuf
Wire: com.squareup.retrofit2:converter-wire
Simple XML: com.squareup.retrofit2:converter-simplexml
Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars
Retrofit这里以GsonConverterFactory的为例
添加依赖
compile ‘com.squareup.retrofit2:converter-gson:2.0.0-beta4’
定义java bean
- public class IpInfo {
- private int code;
- private DataBean data;
- public int getCode() {
- return code;
- }
- public void setCode(int code) {
- this.code = code;
- }
- public DataBean getData() {
- return data;
- }
- public void setData(DataBean data) {
- this.data = data;
- }
- public static class DataBean {
- private String country;
- private String country_id;
- private String area;
- private String area_id;
- private String region;
- private String region_id;
- private String city;
- private String city_id;
- private String county;
- private String county_id;
- private String isp;
- private String isp_id;
- private String ip;
- public String getCountry() {
- return country;
- }
- public void setCountry(String country) {
- this.country = country;
- }
- public String getCountry_id() {
- return country_id;
- }
- public void setCountry_id(String country_id) {
- this.country_id = country_id;
- }
- public String getArea() {
- return area;
- }
- public void setArea(String area) {
- this.area = area;
- }
- public String getArea_id() {
- return area_id;
- }
- public void setArea_id(String area_id) {
- this.area_id = area_id;
- }
- public String getRegion() {
- return region;
- }
- public void setRegion(String region) {
- this.region = region;
- }
- public String getRegion_id() {
- return region_id;
- }
- public void setRegion_id(String region_id) {
- this.region_id = region_id;
- }
- public String getCity() {
- return city;
- }
- public void setCity(String city) {
- this.city = city;
- }
- public String getCity_id() {
- return city_id;
- }
- public void setCity_id(String city_id) {
- this.city_id = city_id;
- }
- public String getCounty() {
- return county;
- }
- public void setCounty(String county) {
- this.county = county;
- }
- public String getCounty_id() {
- return county_id;
- }
- public void setCounty_id(String county_id) {
- this.county_id = county_id;
- }
- public String getIsp() {
- return isp;
- }
- public void setIsp(String isp) {
- this.isp = isp;
- }
- public String getIsp_id() {
- return isp_id;
- }
- public void setIsp_id(String isp_id) {
- this.isp_id = isp_id;
- }
- public String getIp() {
- return ip;
- }
- public void setIp(String ip) {
- this.ip = ip;
- }
- }
- }
接口方法声明
- //GSON转换数据
- @GET("http://ip.taobao.com/service/getIpInfo.php")
- Call<IpInfo> getIpInfo2(@Query("ip") String ip);
调用接口
- Call<IpInfo> ipInfoCall = apiStores.getIpInfo2("220.160.193.207");
- ipInfoCall.enqueue(new Callback<IpInfo>() {
- @Override
- public void onResponse(Response<IpInfo> response) {
- Log.d("onResponse",response.body().getData().getCity());
- }
- @Override
- public void onFailure(Throwable t) {
- Log.i("onFailure", "onFailure=" + t.getMessage()); }
- });
进阶使用2: 常用接口范例声明
- //这里url为请求地址
- //多参数,用map,注解用@QueryMap
- @GET("url")
- Call<ResponseBody> getInfo(@QueryMap Map<String,String> params);
- //post的请求参数是放在请求体中的,就是body内(详见http请求),这是以json格式传递参数的
- @POST("url")
- @FormUrlEncoded
- Call<ResponseBody> doLogin(@Body User user);
- //post表单传递,map,就是我们一般用到的
- @POST("url")
- @FormUrlEncoded
- Call<ResponseBody> doLogin(@FieldMap Map<String,String> params);
- //也是post表单传递,是以单个进行传递
- @FormUrlEncoded
- @POST("url")
- Call<ResponseBody> doLogin(@Field("username") String name, @Field("password") String password);
- //请求头更改
- @FormUrlEncoded
- @Headers({"Accept: application/vnd.github.v3.full+json",
- "User-Agent: Retrofit-Sample-App"})
- Call<ResponseBody> getUserInfo();
- //动态改变请求头
- @GET("/user")
- Call<User> getUser(@Header("Authorization")
- String authorization);
0 0
- [置顶] 浅谈我为什么选择用Retrofit作为我的网络请求框架
- 浅谈我为什么选择用Retrofit作为我的网络请求框架
- 我为什么选择VC6作为我的开发环境
- 我为什么选择Firefox作为主力浏览器
- retrofit网络请求框架
- 网络请求框架:Retrofit
- 网络请求框架Retrofit
- Retrofit网络请求框架
- retrofit-rxAndroid-最快最好用的网络请求框架
- 网络请求框架Retrofit的基本使用
- Retrofit网络请求框架的基本使用
- 网络框架Retrofit的Get请求
- Retrofit网络请求框架,简介
- Retrofit+okhttp网络框架请求
- Retrofit+RxJava网络请求框架
- 选择Eclipse作为我的主要IDE
- android网络请求框架的学习之路——浅谈对Volley,OkHttp,Retrofit的了解与对比 标签: androidVolleyOkhttpRetrofit网络请求框架
- android网络请求框架的学习之路——浅谈对Volley,OkHttp,Retrofit的了解与对比
- 【游戏设计模式】之四 《游戏编程模式》全书内容提炼总结
- Ubuntu16.04下安装sublime text3并配置anaconda插件
- 华山论剑之契约式编程与防御式编程
- 二叉树利用队列实现层次遍历
- WiFi 网络也可以这么玩
- [置顶] 浅谈我为什么选择用Retrofit作为我的网络请求框架
- 软件工程(C编码实践篇)学习总结
- leetcode-17. Letter Combinations of a Phone Number
- 软件工程C编码实践课程总结
- 沃通的证书受到质疑,StartCom CA 受处罚
- C/C++初学者项目开发实践1
- 软件工程(C编码实践篇)学习总结
- 分布式系统--2个层次/2个境界
- 2016ACM/ICPC亚洲区青岛站 ABC题题解