原文连接:http://www.javashuo.com/article/p-phtjrmli-de.htmlhtml
4.一、Retrofit网络请求框架
概念:Retrofit是一个基于RESTful的HTTP网络请求框架的封装,其中网络请求的本质是由OKHttp完成的,而Retrofit仅仅负责网络请求接口的封装。git
原理:App应用程序经过Retrofit请求网络,其实是使用Retrofit接口层封装请求参数,Header、URL等信息,以后由OKHttp完成后续的请求,在服务器返回数据以后,OKHttp将原始的结果交给Retrofit,最后根据用户的需求对结果进行解析。github
retrofit使用
1.在retrofit中经过一个接口做为http请求的api接口
`public interface NetApi {算法
@GET("repos/{owner}/{repo}/contributors") Call<ResponseBody> contributorsBySimpleGetCall(@Path("owner") String owner, @Path("repo") String repo);
}
`
2.建立一个Retrofit实例
`Retrofit retrofit = new Retrofit.Builder()json
.baseUrl("https://api.github.com/") .build();
`
3.调用api接口
`NetApi repo = retrofit.create(NetApi.class);api
//第三步:调用网络请求的接口获取网络请求
retrofit2.Call<ResponseBody> call = repo.contributorsBySimpleGetCall("username", "path");
call.enqueue(new Callback<ResponseBody>() { //进行异步请求缓存
@Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { //进行异步操做 } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { //执行错误回调方法 }
});
`
retrofit动态代理
retrofit执行的原理以下:
1.首先,经过method把它转换成ServiceMethod。
2.而后,经过serviceMethod,args获取到okHttpCall对象。
3.最后,再把okHttpCall进一步封装并返回Call对象。
首先,建立retrofit对象的方法以下:
`Retrofit retrofit = new Retrofit.Builder()服务器
.baseUrl("https://api.github.com/") .build();
`
在建立retrofit对象的时候用到了build()方法,该方法的实现以下:
`public Retrofit build() {
if (baseUrl == null) {网络
throw new IllegalStateException("Base URL required.");
}框架
okhttp3.Call.Factory callFactory = this.callFactory;
if (callFactory == null) {
callFactory = new OkHttpClient(); //设置kHttpClient
}
Executor callbackExecutor = this.callbackExecutor;
if (callbackExecutor == null) {
callbackExecutor = platform.defaultCallbackExecutor(); //设置默认回调执行器
}
// Make a defensive copy of the adapters and add the default Call adapter.
List<CallAdapter.Factory> adapterFactories = new ArrayList<>(this.adapterFactories);
adapterFactories.add(platform.defaultCallAdapterFactory(callbackExecutor));
// Make a defensive copy of the converters.
List<Converter.Factory> converterFactories = new ArrayList<>(this.converterFactories);
return new Retrofit(callFactory, baseUrl, converterFactories, adapterFactories,
callbackExecutor, validateEagerly); //返回新建的Retrofit对象
}
`
该方法返回了一个Retrofit对象,经过retrofit对象建立网络请求的接口的方式以下:
`NetApi repo = retrofit.create(NetApi.class);
`
retrofit对象的create()方法的实现以下:
`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(); @Override 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); //获取ServiceMethod对象 OkHttpCall okHttpCall = new OkHttpCall<>(serviceMethod, args); //传入参数生成okHttpCall对象 return serviceMethod.callAdapter.adapt(okHttpCall); //执行okHttpCall } });
}
`
4.二、图片加载库对比
Picasso:120K
Glide:475K
Fresco:3.4M
Android-Universal-Image-Loader:162K
图片函数库的选择须要根据APP的具体状况而定,对于严重依赖图片缓存的APP,例如壁纸类,图片社交类APP来讲,能够选择最专业的Fresco。对于通常的APP,选择Fresco会显得比较重,毕竟Fresco3.4M的体量摆在这。根据APP对图片的显示和缓存的需求从低到高,咱们能够对以上函数库作一个排序。
Picasso < Android-Universal-Image-Loader < Glide < Fresco
2.介绍:
Picasso :和Square的网络库一块儿能发挥最大做用,由于Picasso能够选择将网络请求的缓存部分交给了okhttp实现。
Glide:模仿了Picasso的API,并且在他的基础上加了不少的扩展(好比gif等支持),Glide默认的Bitmap格式是RGB_565,比 Picasso默认的ARGB_8888格式的内存开销要小一半;Picasso缓存的是全尺寸的(只缓存一种),而Glide缓存的是跟ImageView尺寸相同的(即5656和128128是两个缓存) 。
FB的图片加载框架Fresco:最大的优点在于5.0如下(最低2.3)的bitmap加载。在5.0如下系统,Fresco将图片放到一个特别的内存区域(Ashmem区)。固然,在图片不显示的时候,占用的内存会自动被释放。这会使得APP更加流畅,减小因图片内存占用而引起的OOM。为何说是5.0如下,由于在5.0之后系统默认就是存储在Ashmem区了。
3.总结:
Picasso所能实现的功能,Glide都能作,无非是所需的设置不一样。可是Picasso体积比起Glide小太多若是项目中网络请求自己用的就是okhttp或者retrofit(本质仍是okhttp),那么建议用Picasso,体积会小不少(Square全家桶的干活)。Glide的好处是大型的图片流,好比gif、Video,若是大家是作美拍、爱拍这种视频类应用,建议使用。
Fresco在5.0如下的内存优化很是好,代价就是体积也很是的大,按体积算Fresco>Glide>Picasso
不过在使用起来也有些不便(小建议:他只能用内置的一个ImageView来实现这些功能,用起来比较麻烦,咱们一般是根据Fresco本身改改,直接使用他的Bitmap层)
4.三、各类json解析库使用
参考连接:https://www.cnblogs.com/kunpe...
(1)Google的Gson
Gson是目前功能最全的Json解析神器,Gson当初是为因应Google公司内部需求而由Google自行研发而来,但自从在2008年五月公开发布初版后已被许多公司或用户应用。Gson的应用主要为toJson与fromJson两个转换函数,无依赖,不须要例外额外的jar,可以直接跑在JDK上。而在使用这种对象转换以前需先建立好对象的类型以及其成员才能成功的将JSON字符串成功转换成相对应的对象。类里面只要有get和set方法,Gson彻底能够将复杂类型的json到bean或bean到json的转换,是JSON解析的神器。Gson在功能上面无可挑剔,可是性能上面比FastJson有所差距。
(2)阿里巴巴的FastJson
Fastjson是一个Java语言编写的高性能的JSON处理器,由阿里巴巴公司开发。
无依赖,不须要例外额外的jar,可以直接跑在JDK上。FastJson在复杂类型的Bean转换Json上会出现一些问题,可能会出现引用的类型,致使Json转换出错,须要制定引用。FastJson采用首创的算法,将parse的速度提高到极致,超过全部json库。
综上Json技术的比较,在项目选型的时候可使用Google的Gson和阿里巴巴的FastJson两种并行使用,若是只是功能要求,没有性能要求,可使用google的Gson,若是有性能上面的要求可使用Gson将bean转换json确保数据的正确,使用FastJson将Json转换Bean
点击下方连接免费获取Android进阶资料:
https://shimo.im/docs/tXXKHgdjPYj6WT8d/