加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 业界 > 正文

Android第三方库解析

发布时间:2019-07-30 19:30:17 所属栏目:业界 来源:Engineers
导读:前言 很多人面试之前,可能没有在互联网公司工作过或者说工作过但年头较短,不知道互联网公司技术面试都会问哪些问题? 再加上可能自己准备也不充分,去面试没几个回合就被面试官几个问题打蒙了,最后以惨败收
副标题[/!--empirenews.page--]

前言

很多人面试之前,可能没有在互联网公司工作过或者说工作过但年头较短,不知道互联网公司技术面试都会问哪些问题? 再加上可能自己准备也不充分,去面试没几个回合就被面试官几个问题打蒙了,最后以惨败收场。

Android第三方库解析

1.Retrofit网络请求框架

概念:Retrofit是一个基于RESTful的HTTP网络请求框架的封装,其中网络请求的本质是由OKHttp完成的,而Retrofit仅仅负责网络请求接口的封装。

原理:App应用程序通过Retrofit请求网络,实际上是使用Retrofit接口层封装请求参数,Header、URL等信息,之后由OKHttp完成后续的请求,在服务器返回数据之后,OKHttp将原始的结果交给Retrofit,最后根据用户的需求对结果进行解析。

retrofit使用

1.在retrofit中通过一个接口作为http请求的api接口

public interface NetApi { @GET("repos/{owner}/{repo}/contributors") Call contributorsBySimpleGetCall(@Path("owner") String owner, @Path("repo") String repo);}

2.创建一个Retrofit实例

Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .build();

3.调用api接口

  1. NetApi repo = retrofit.create(NetApi.class); 
  2. //第三步:调用网络请求的接口获取网络请求 
  3. retrofit2.Call<ResponseBody> call = repo.contributorsBySimpleGetCall("username", "path"); 
  4. call.enqueue(new Callback<ResponseBody>() { //进行异步请求 
  5.  @Override 
  6.  public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
  7.  //进行异步操作 
  8.  } 
  9.  @Override 
  10.  public void onFailure(Call<ResponseBody> call, Throwable t) { 
  11.  //执行错误回调方法 
  12.  } 
  13. }); 

retrofit动态代理

retrofit执行的原理如下:

  • 首先,通过method把它转换成ServiceMethod。
  • 然后,通过serviceMethod,args获取到okHttpCall对象。
  • 最后,再把okHttpCall进一步封装并返回Call对象。 首先,创建retrofit对象的方法如下:
  1. Retrofit retrofit = new Retrofit.Builder() 
  2.  .baseUrl("https://api.github.com/") 
  3.  .build(); 

在创建retrofit对象的时候用到了build()方法,该方法的实现如下:

  1. public Retrofit build() { 
  2.  if (baseUrl == null) { 
  3.  throw new IllegalStateException("Base URL required."); 
  4.  } 
  5.  okhttp3.Call.Factory callFactory = this.callFactory; 
  6.  if (callFactory == null) { 
  7.  callFactory = new OkHttpClient(); //设置kHttpClient 
  8.  } 
  9.  Executor callbackExecutor = this.callbackExecutor; 
  10.  if (callbackExecutor == null) { 
  11.  callbackExecutor = platform.defaultCallbackExecutor(); //设置默认回调执行器 
  12.  } 
  13.  // Make a defensive copy of the adapters and add the default Call adapter. 
  14.  List<CallAdapter.Factory> adapterFactories = new ArrayList<>(this.adapterFactories); 
  15.  adapterFactories.add(platform.defaultCallAdapterFactory(callbackExecutor)); 
  16.  // Make a defensive copy of the converters. 
  17.  List<Converter.Factory> converterFactories = new ArrayList<>(this.converterFactories); 
  18.  return new Retrofit(callFactory, baseUrl, converterFactories, adapterFactories, 
  19.  callbackExecutor, validateEagerly); //返回新建的Retrofit对象 

该方法返回了一个Retrofit对象,通过retrofit对象创建网络请求的接口的方式如下:

  1. NetApi repo = retrofit.create(NetApi.class); 

retrofit对象的create()方法的实现如下:

  1. public <T> T create(final Class<T> service) { 
  2.  Utils.validateServiceInterface(service); 
  3.  if (validateEagerly) { 
  4.  eagerlyValidateMethods(service); 
  5.  } 
  6.  return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[] { service }, 
  7.  new InvocationHandler() { 
  8.  private final Platform platform = Platform.get(); 
  9.  @Override public Object invoke(Object proxy, Method method, Object... args) 
  10.  throws Throwable { 
  11.  // If the method is a method from Object then defer to normal invocation. 
  12.  if (method.getDeclaringClass() == Object.class) { 
  13.  return method.invoke(this, args); //直接调用该方法 
  14.  } 
  15.  if (platform.isDefaultMethod(method)) { 
  16.  return platform.invokeDefaultMethod(method, service, proxy, args); //通过平台对象调用该方法 
  17.  } 
  18.  ServiceMethod serviceMethod = loadServiceMethod(method); //获取ServiceMethod对象 
  19.  OkHttpCall okHttpCall = new OkHttpCall<>(serviceMethod, args); //传入参数生成okHttpCall对象 
  20.  return serviceMethod.callAdapter.adapt(okHttpCall); //执行okHttpCall 
  21.  } 
  22.  }); 

2.图片加载库对比

  • Picasso:120K
  • Glide:475K
  • Fresco:3.4M

Android-Universal-Image-Loader:162K

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读