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

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接口
- NetApi repo = retrofit.create(NetApi.class);
- //第三步:调用网络请求的接口获取网络请求
- 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执行的原理如下:
- 首先,通过method把它转换成ServiceMethod。
- 然后,通过serviceMethod,args获取到okHttpCall对象。
- 最后,再把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
- }
- });
- }
2.图片加载库对比
- Picasso:120K
- Glide:475K
- Fresco:3.4M
Android-Universal-Image-Loader:162K
(编辑:晋中站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|