副问题[/!--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
(编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|