目录:andorid jar/库源码解析

Retrofit2:

  作用:

    通过封装okhttp库,来进行web通讯,并且使用动态代理的方式,来调用接口地址,通过回调赋值结果。

  栗子:

  定义一个接口,用于访问使用。

public interface IServiceApi {
@FormUrlEncoded
@POST("login")
Call<LoginResult> login(@Field("name") String name, @Field("pwd") String pwd); @GET("getinfo")
Call<UserInfo> getinfo(@Query("token") String token); @GET("getinfo2")
Call<UserInfo> getinfo2(@Query("token") String token);
}

  调用接口1.可以在main中调用,因为是通过异步执行(enqueue)

Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl("http://192.168.86.11:8087/").build();
IServiceApi api = retrofit.create(IServiceApi.class);
Call<LoginResult> call = api.login("test", "test1234");
call.enqueue(new Callback<LoginResult>() {
@Override
public void onResponse(Call<LoginResult> call, Response<LoginResult> response) {
LoginResult loginResult = response.body();
if(loginResult != null) {
}
} @Override
public void onFailure(Call<LoginResult> call, Throwable t) {
Log.i(tag, "ex " + t.getMessage());
}
});

  调用接口2.可以在main中调用,因为是通过异步执行(enqueue)

Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl("http://192.168.86.11:8087/").build();
IServiceApi api = retrofit.create(IServiceApi.class);
Call<UserInfo> call = api.getinfo("testtesttest");
call.enqueue(new Callback<UserInfo>() {
@Override
public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
UserInfo userInfo = response.body();
if(userInfo != null) {
}
} @Override
public void onFailure(Call<UserInfo> call, Throwable t) {
Log.i(tag, "ex " + t.getMessage());
}
});

  同步调用接口3.

Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl("http://192.168.86.11:8087/").build();
IServiceApi api = retrofit.create(IServiceApi.class);
Call<LoginResult> call = api.login("test", "test1234");
try {
Response<LoginResult> resultResponse = call.execute();
LoginResult result = resultResponse.body();
}catch (Exception e){
e.printStackTrace();
}

  源码解读:

  A:异步调用

  1、创建一个Retrofit对象。

  2、retrofit.create(IServiceApi.class); // 使用Proxy.newProxyInstance 创建一个接口的代理对象。内部使用 ServiceMethod配合OkHttpCall调用,构造他们需要的对象数据

  3、调用enqueue,内部构造okhttp3.Call对象,执行对象的enqueue方法。然后在okhttp3$Callback方法中,调用retrofit2的 回调,赋值成功和失败。

  B:同步调用

  1、同理,同步调用,最后也是调用的。okhtt3.Call的execute方法。

  源码:https://github.com/square/retrofit

  引入:

implementation 'com.squareup.retrofit2:retrofit:2.0.2'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.2'

andorid jar/库源码解析之retrofit2的更多相关文章

  1. andorid jar/库源码解析之Bolts

    目录:andorid jar/库源码解析 Bolts: 作用: 用于链式执行跨线程代码,且传递数据 栗子: Task.call(new Callable<Boolean>() { @Ove ...

  2. andorid jar/库源码解析之EventBus

    目录:andorid jar/库源码解析 EventBus: 作用: 用于不同Activity,Service等之间传递消息(数据). 栗子: A页面:onCreate定义   EventBus.ge ...

  3. andorid jar/库源码解析之Dagger/Dagger2

    目录:andorid jar/库源码解析 Dagger.Dagger2: 作用: 1.用于解耦Activity和业务逻辑 2.在使用业务的时候,不需要重复编写new代码. 3.当业务变化的时候,不需要 ...

  4. andorid jar/库源码解析之okhttp3

    目录:andorid jar/库源码解析 Okhttp3: 作用: 用于网络编程(http,https)的快速开发. 栗子: // okHttpClient定义成全局静态,或者单例,不然重复new可能 ...

  5. andorid jar/库源码解析之okio

    目录:andorid jar/库源码解析 Okio: 作用: 说白了,就是一个IO库,基于java原生io.来进行操作,内部做了优化,简洁,高效.所以受到了一部分人的喜欢和使用 栗子: 读写文件. p ...

  6. andorid jar/库源码解析之Butterknife

    目录:andorid jar/库源码解析 Butterknife: 作用: 用于初始化界面控件,控件方法,通过注释进行绑定控件和控件方法 栗子: public class MainActivity e ...

  7. andorid jar/库源码解析之zxing

    目录:andorid jar/库源码解析 Zxing: 作用: 生成和识别,二维码,条形码. 栗子: 生成二维码,赋值到ImageView上 QRCodeWriter qrCodeWriter = n ...

  8. andorid jar/库源码解析之错误提示

    目录:andorid jar/库源码解析 错误: 错误1: Error: Static interface methods are only supported starting with Andro ...

  9. andorid jar/库源码解析

    前言 本篇作为开篇,会大体上说明,需要解读源码的,类库,或者jar. 序 原本,类库和jar的系列准备写到逆向系列课程的,但是那个东西,在写了两篇,就没有后续了,现在也不知道从哪里开始了, 只能等后期 ...

随机推荐

  1. list[列表]的使用

    #!/usr/bin/env python3# -*- coding:utf-8 -*-# name:zzyushop_list = [["手机",5000], ["电脑 ...

  2. MyBatis-Plus使用小结

    官网: https://mybatis.plus/ https://gitee.com/baomidou/mybatis-plus https://github.com/baomidou/mybati ...

  3. AJ学IOS 之微博项目实战(4)微博自定义tabBar中间的添加按钮

    AJ分享,必须精品 一:效果图 自定义tabBar实现最下面中间的添加按钮 二:思路 首先在自己的tabBarController中把系统的tabBar设置成自己的tabBar(NYTabBar),这 ...

  4. layoutInflater参数解析与源码分析

    关于LayoutInflater方法,无论是在listview的适配器中,还是在动态添加view的时候,都会出现它的身影,最开始我在看<第一行代码>时,不知道这个方法实际的参数到底指的是什 ...

  5. Candy Distribution

    Kids like candies, so much that they start beating each other if the candies are not fairly distribu ...

  6. XML-解析失败原因初步分析

    更多精彩文章请关注公众号『大海的BLOG』 首先放出有问题的代码 之所以直入主题是因为肝完了事情,急需入睡.hiahia hiboard:updateUrl="https://xxx.com ...

  7. 详解 Map集合

    (请关注 本人"集合总集篇"博文--<详解 集合框架>) 首先,本人来讲解下 Map集合 的特点: Map集合 的特点: 特点: 通过 键 映射到 值的对象 一个 映射 ...

  8. jmeter DB2数据库连接与操作

    1.需要把数据库连接jar包拷贝到 jmeter lib目录下 先创建一个数据库连接配置元件 2.添加jdbc请求(我用的后置处理器) 3.可以通过beanshell 对结果集进行操作 beanshe ...

  9. 解决报错:JPA-style positional param was not an integral ordinal;

    org.hibernate.QueryException: JPA-style positional param was not an integral ordinal; nested excepti ...

  10. vim环境下空格和tab键互换

    对于已保存的文件,可以使用下面的方法进行空格和TAB的替换 TAB替换为空格::set ts=4:set expandtab:%retab! 空格替换为TAB::set ts=4:set noexpa ...