目录: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. 10.1 io流--ASCII码表

    day2.8中提到 /* * +: * 做加法运算 * * 字符参与加法运算,其实是拿字符在计算机中存储的数据值来参与运算的 * 'A' 65(B 66...) * 'a' 97(b 98...) * ...

  2. 9.1 ArrayList(集合)的使用,与array(数组)的对比

    1.array 和ArrayList的区别? array 数组的长度是固定的,适应不了变化的需求. ArrayList集合的长度可变.大小可变. 2.为什么要用集合,它优点是什么? java是面向对象 ...

  3. JAVA debug 调试demo

    1.设置断点,在代码的行号后面鼠标左键即可2.想要看调用方法的执行流程,那么调用方法也要加断点. package day6_debug; /* * 1.设置断点,在代码的行号后面鼠标左键即可 * 2. ...

  4. Python Requests-学习笔记(3)-处理json

    JSON响应内容 Requests中也有一个内置的JSON解码器,助你处理JSON数据: r = requests.get('https://github.com/timeline.json') pr ...

  5. thymeleaf的特殊属性赋值

    在用thymeleaf时,遇到特殊属性不知道该怎么解决如下: 问题1:循环时,遇到特殊的属性,不知道怎么赋值 如:cate-id="" ,fid=""; 使用t ...

  6. AJ学IOS 之CoreLocation指南针小应用

    AJ分享,必须精品 一:效果图示 简单的用到CoreLocation获取方位做的指南针小应用 二:制作思路 具体用到了CoreLocation相关的知识,请看上一篇博客有写 然后获取方向不需要进行授权 ...

  7. AJ学IOS(53)多线程网络之NSOperation简介

    AJ分享,必须精品 一:简单介绍 1:NSOperation的作⽤使用步骤: 配合使用NSOperation和NSOperationQueue也能实现多线程编程. NSOperation和NSOper ...

  8. day7作业

    # day7作业 # 1. 使用while循环输出1 2 3 4 5 6 8 9 10 count = 1 while count < 11: if count == 7: count += 1 ...

  9. HashMap之KeySet分析

    本篇涵盖 1.HashMap并不是用keySet来存储key的原因及证明 2.keySet方法返回后的remove.add操作原理 一.方法作用 概括一下 1.keySet方法返回map中包含的键的集 ...

  10. Nightmare BFS

    Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The la ...