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

Android第三方库解析

发布时间:2019-07-30 20:57:57 所属栏目:业界 来源: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

(编辑:湖南网)

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

热点阅读