//next传到当前节点,当前节点处理好request后就可以通过next执行proceed方法,将request传递到下一节点
Interceptor interceptor = interceptors.get(index);
Response response = interceptor.intercept(next);
return response;
}
}
拦截器
public class BridgeInterceptor implements Interceptor{
@Override
public Response intercept(Chain chain) throws IOException {
Log.e(“TAG”,“BridgeInterceptor”);
Request request = chain.request();
// 添加一些请求头
request.header(“Connection”,“keep-alive”);
// 做一些其他处理
if(request.requestBody()!=null){
RequestBody requestBody = request.requestBody();
request.header(“Content-Type”,requestBody.getContentType());
request.header(“Content-Length”,Long.toString(requestBody.getContentLength()));
}
Response response = chain.proceed(request);//这里的chain就是传进来的next,next的index已经加1
return response;
}
}
RealCall中excute()方法
protected void execute() {
final Request request = orignalRequest;
try {
List interceptors = new ArrayList<>();
interceptors.add(new BridgeInterceptor());
interceptors.add(new CacheInterceptor());
interceptors.add(new CallServerInterceptor());
Interceptor.Chain chain = new RealInterceptorChain(interceptors,0,orignalRequest);
Response response = chain.proceed(request);
callback.onResponse(RealCall.this,response);
} catch (IOException e) {
callback.onFailure(RealCall.this,e);
}
}
算法
1.反转单链表
=======
class Node{
private int data;
private Node next;
public Node(int data,Node next){
this.data=data;
this.next=next;
}
}
Node node4 = new Node(4, null);
Node node3 = new Node(3, node4);
Node node2 = new Node(2, node3);
Node node1 = new Node(1, node2);
Node pHead = node1;//头结点
这组链表从1到4排序,要求反转后4到1
public static Node reverseList(Node pHead) {
Node pReversed 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》无偿开源 徽信搜索公众号【编程进阶路】 Head = null; //反转过后的单链表存储头结点
Node pNode = pHead; //当前节点
Node pPrev = null; //前一结点
while (pNode != null) {
//1.记录next,下一步:更新当前节点的上一节点和本身。最后移动一位
Node pNext = pNode.next;
if (pNext == null) {
//到了尾节点
pReversedHead = pNode;
}
pNode.next = pPrev;
pPrev = pNode;
pNode = pNext;
}
return pReversedHead;
}
//递归方式反转(node1->node2->node3->node4->node5)
public static ListNode reverseR(Node head){
//空链表和一个结点的链表无需反转
if(head == null || head.next == null){
return head;
}
//递归到node4(head)时回溯时,reverseR(node4.next)直接返回node5
Node res = reverseR(head.next);
//递归回溯时,此时head指向node4,将node4的next(node5)的next指向node4(head)
head.next.next = head;
//将node4.next指向null
head.next = null;
return res;
}
输出
pHead = reverseList(pHead);//反转之后头结点
while (pHead != null) {
System.out.println(pHead.key);
pHead = pHead.next;
}
2.LRU算法(最近最少使用算法)
=================
-
可以在存储不足时移除掉最近最少使用的数据
-
使用哈希链表(LinkedHashMap)把数据按照最后使用时间来排序。最新使用的数据插入(移到)链表最前端
其他
1.Https
=======
-
使用对称密钥:加密和解密使用的是同一个密钥。弊端:最开始的时候怎么将这个对称密钥发送出去呢?如果对称密钥在发送的时候就已经被拦截,那么发送的信息还是会被篡改和窥视
-
使用非对称密钥:双方必须协商一对密钥,一个私钥一个公钥。用私钥加密的数据,只有对应的公钥才能解密,用公钥加密的数据, 只有对应的私钥才能解密。A将自己的公钥发给B,B以后给A发消息时候用公钥加密后发送,A收到消息后用自己的私钥解密。弊端:非对称密钥(RSA)加密和解密速度慢
-
非对称密钥+对称密钥:A将自己的公钥发给B,B用公钥将对称密钥加密发给B,这样双方就安全地传递了对称加密的密钥,既解决了密钥的传递问题, 又解决了RSA速度慢的问题。弊端:第一次传递公钥时有风险
-
数字证书。假如一开始A将自己的公钥发给B时,中间被拦截,拦截者替换成自己的公钥,这样还是会有安全问题。解决办法:数字证书
2.三次握手