Retrofit是Square公司发布的一个能够应用在Android和Java中的Http客户端访问框架,其底层应用的是OkHttp。html
在这个帖子中,咱们如下面这个Http请求为例:git
https://api.github.com/users/basil2style
其请求结果(JSON)以下所示:程序员
{ "login": "basil2style", "id": 1285344, "avatar_url": "https://avatars.githubusercontent.com/u/1285344?v=3", "gravatar_id": "", "url": "https://api.github.com/users/basil2style", "html_url": "https://github.com/basil2style", "followers_url": "https://api.github.com/users/basil2style/followers", "following_url": "https://api.github.com/users/basil2style/following{/other_user}", "gists_url": "https://api.github.com/users/basil2style/gists{/gist_id}", "starred_url": "https://api.github.com/users/basil2style/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/basil2style/subscriptions", "organizations_url": "https://api.github.com/users/basil2style/orgs", "repos_url": "https://api.github.com/users/basil2style/repos", "events_url": "https://api.github.com/users/basil2style/events{/privacy}", "received_events_url": "https://api.github.com/users/basil2style/received_events", "type": "User", "site_admin": false, "name": "Basil", "company": "MakeInfo", "blog": "http://www.themakeinfo.com", "location": "Toronto,Canada", "email": "basiltalias92@gmail.com", "hireable": true, "bio": "Developer | Marketer | Reader | Cinephile | Entrepreneur", "public_repos": 35, "public_gists": 4, "followers": 64, "following": 155, "created_at": "2011-12-26T00:17:22Z", "updated_at": "2016-11-12T00:58:15Z" }
接下来咱们从Retrofit的用法到原理,来介绍一下这个框架。github
1、Retrofit的用法:api
0、配置Retrofit的开发环境:服务器
咱们在使用Retrofit以前须要先导入Retrofit的包,本DEMO是在Android Studio中开发的,所以只须要在gradle文件中导入依赖便可。下面是Retrofit的依赖:网络
compile 'com.squareup.retrofit2:retrofit:2.1.0'
另外,咱们从网络中获取到的数据的格式是不一样的,多是JSON/GSON格式,也多是Jackson、Wire等其余格式,咱们须要在后面的编码中用到一个格式转化工厂ConverterFactory,所以咱们还须要导入一些有关格式的依赖。本DEMO中使用的是JSON/GSON格式,所以导入相关依赖以下:框架
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
其余格式须要导入的依赖列表以下(后面的版本号本身添加):异步
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的开发环境搭建就完成了。ide
一、建立一个从网络中获取数据的Service接口:
Retrofit使用动态代理的方式,将一个网络请求转换成一个接口,用户只须要调用一个接口就能够进行网络访问。这个接口的代码以下:
public interface RetrofitService { @GET("/users/basil2style") Call<InfoData> getInfoData(); }
从上面这段代码中能够看出来, @GET 标签中的值只是这个请求的一部分。通常咱们在进行网络请求的时候,大多数请求都是从一个服务器发送的数据,所以咱们能够对这些请求的根URL进行统一的管理。在这个DEMO中,我把网络请求的根URL放在SharedData类中:
public class SharedData { /** * Base Url */ public static final String BASE_URL = "https://api.github.com"; }
和其余网络请求相同,Retrofit能够经过 @GET 和 @POST 两种方法进行网络请求,这些请求有时候须要携带参数,有时候不须要,上面的getInfoData()方法就是一个不携带任何参数的网络请求方法。固然,这里还须要建立一个Bean类InfoData,用来存储从网络中获取到的数据:
public class InfoData { private String login; private int id; private String avatarUrl; private String gravatarId; private String url; private String htmlUrl; private String followersUrl; private String followingUrl; private String gistsUrl; private String starredUrl; private String subscriptionsUrl; private String organizationsUrl; private String reposUrl; private String eventsUrl; private String receivedEventsUrl; private String type; private boolean siteAdmin; private String name; private String company; private String blog; private String location; private String email; private boolean hireable; private String bio; private int publicRepos; private int publicGists; private int followers; private int following; private String createdAt; private String updatedAt; // Getter、Setter方法 }
再来介绍一下Call。Call能够用来发送一个请求,Call对象中有两个方法:enqueue()和execute(),前者用来发送一个异步请求,后者又来发送一个同步请求,下面会有代码介绍。
二、初始化Retrofit对象:
建立Retrofit对象的代码以下:
Retrofit retrofit = new Retrofit.Builder() .baseUrl(SharedData.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build();
能够看到,Retrofit对象是经过Retrofit.Builder内部类建立出来的,baseUrl是须要访问的根URL;因为请求的数据是JSON格式的,咱们可使用一个和JSON有关的转换工厂来处理这些数据,这里用到的是和JSON差很少的GSON转化工厂,即GsonConverterFactory。
三、调用Retrofit对象进行网络访问:
获取到Retrofit对象后,咱们经过这个对象获取到网络请求接口RetrofitService,再调用其中的getInfoData()方法获取到网络数据便可。具体代码以下:
RetrofitService service = retrofit.create(RetrofitService.class); Call<InfoData> call = service.getInfoData(); call.enqueue(new Callback<InfoData>() { @Override public void onResponse(Call<InfoData> call, Response<InfoData> response) { InfoData data = response.body(); Message message = Message.obtain(); message.what = 1; message.obj = data; handler.sendMessage(message); } @Override public void onFailure(Call<InfoData> call, Throwable t) { handler.sendEmptyMessage(0); } });
这里还须要说明一下,Retrofit不像Volley能够直接把异步数据拉回到主线程,Retrofit中Callback类的onResponse()方法仍然是在异步线程中的,若是咱们要将数据拿到主线程,须要使用AsyncTask或Handler,本DEMO中使用的是Handler,如下是Handler中的代码:
private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 1: InfoData data = (InfoData) msg.obj; Toast.makeText(MainActivity.this, data.getBlog(), Toast.LENGTH_SHORT).show(); break; case 0: Toast.makeText(MainActivity.this, "获取数据失败", Toast.LENGTH_SHORT).show(); break; } } };
到此为止Retrofit访问网络获取数据的DEMO就完成了,运行结果以下图所示:
2、RetrofitService接口方法种类:
上面的RetrofitService接口中的getInfoData()方法只是一个没有携带任何参数的GET请求,咱们在访问网络数据的时候有时须要使用POST请求,而且可能会携带一些参数等,下面来逐个介绍一下这些状况下接口方法的编写方式。
特别说明:下面访问的接口和上面DEMO中的接口没有任何关系,两两之间也没有任何关系!
一、没有参数的GET请求:
@GET("users/list")
Call<List<User>> getUserList();
二、有参数的GET请求:
(1)第一种方式:在GET标签中添加参数:
@GET("users/list?sort=desc")
Call<List<User>> getUserList();
(2)第二种方式:在方法参数中设置参数:
@GET("users/list")
Call<List<User>> getUserList(@Query("sort") String sort);
三、在GET标签中设置路径参数:
@GET("group/{id}/users") Call<List<User>> groupList(@Path("id") int groupId);
四、传入参数列表:
@GET("group/{id}/users") Call<List<User>> groupList(@Path("id") int groupId, @QueryMap Map<String, String> options);
五、POST方式请求:
@POST("users/new")
Call<User> createUser(@Body User user);
3、Retrofit原理:
Retrofit用注解来描述一个网络请求,将一个网络请求抽象成一个接口,而后使用Java动态代理的方式动态的将这个接口“翻译”成一个网络请求,最后调用OkHttp去执行这个请求。
Retrofit中的动态代理主要体如今下面这行代码:
RetrofitService service = retrofit.create(RetrofitService.class);
create()方法的源码以下:
/** Create an implementation of the API defined by the {@code service} interface. */ public <T> T create(final Class<T> service) { Utils.validateServiceInterface(service); if (validateEagerly) { eagerlyValidateMethods(service); } return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[] { service }, new InvocationHandler() { private final Platform platform = Platform.get(); public Object invoke(Object proxy, Method method, Object... args) throws Throwable { // If the method is a method from Object then defer to normal invocation. if (method.getDeclaringClass() == Object.class) { return method.invoke(this, args); } if (platform.isDefaultMethod(method)) { return platform.invokeDefaultMethod(method, service, proxy, args); } ServiceMethod serviceMethod = loadServiceMethod(method); OkHttpCall okHttpCall = new OkHttpCall<>(serviceMethod, args); return serviceMethod.callAdapter.adapt(okHttpCall); } }); }
可见,所谓的动态代理,就是给程序员一种可能:在执行某个操做(如打开某个Class)以前,插入一段你想要执行的代码。好比你在执行某个操做以前判断用户是否已经登陆,等。
当外界(Activity)经过create()方法打开这个接口并调用其中的方法时,底层就调用了上面这段代码中的invode()方法。这个方法是经过Java反射机制获取到接口中的getInfoData()方法,用这个方法做为参数建立一个ServiceMethod对象,最后使用OkHttp的API调用进行网络访问。
总结一下:Retrofit使用注解的方式将一个网络请求封装成一个接口,而后使用动态代理的方法,在插入的代码中经过反射机制找到接口中的方法,封装到OkHttp中进行网络访问,最后对网络访问获得的Call对象进行enqueue()或execute()操做,在回调的方法中处理网络获取到的数据。
以上就是Retrofit的工做原理。