Android 网络请求框架 Retrofit2.0实践使用总结
来源:互联网 发布:matlab solve 数组 编辑:程序博客网 时间:2024/05/19 08:43
比较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转换工厂
可以帮我们将获取到的数据转换为JAVA BEAN
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
/** * Created by xemenes on 25/03/16. */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")@FormUrlEncodedCall<ResponseBody> doLogin(@Body User user);//post表单传递,map,就是我们一般用到的@POST("url")@FormUrlEncodedCall<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);
其他资料
官方资料
Retrofit2.0 使用归纳和 JSONObject Conveter
Retrofit2 源码解析
1 0
- Android 网络请求框架 Retrofit2.0实践使用总结
- Android 网络请求框架 Retrofit2.0实践使用总结
- Android 网络请求框架Retrofit2.0使用笔记
- Android RxJava2+Retrofit2搭建网络请求框架
- Android使用Retrofit2.0和RxJava2.0处理网络请求
- Android 网络框架 Retrofit2.0介绍、使用和封装
- Android 网络框架 Retrofit2.0介绍、使用和封装
- Android中网络框架Retrofit2.0简单使用
- Android 实现Retrofit2.0请求网络
- 【Android 进阶】Retrofit2 目前最优雅的网络请求框架
- RxJava2+Retrofit2网络请求框架封装及使用
- Android-网络框架04Retrofit2.0+RxJava
- 使用retrofit2.0实现网络请求post和get请求
- 网络请求框架之Retrofit2.0基本讲解
- 如何使用retrofit2网络框架
- Retrofit2网络框架的使用
- Retrofit2+Rxjava2网络请求框架的封装
- Android网络请求框架之Retrofit实践
- C语言实现顺序表的插入、删除、查找、遍历等基本操作
- 发现群组(三)根据偏好聚类
- Windows下安装python问题汇总
- 5-21 Hashing (25分)
- HDU2196 Computer(树形DP)
- Android 网络请求框架 Retrofit2.0实践使用总结
- centos7 常用命令
- .Net AOP(五) 各种IoC框架下实现AOP
- 文章标题
- android api-level
- leetcode 32. Longest Valid Parentheses
- Java实现二叉树
- 减治法-两个序列的中位数
- 5-24 地下迷宫探索 (30分)