android-12 okhttp interview

Summarized in one sentence:

OKHttp3 intercepted by designing chain, so the request interceptor is divided into five to handle, their duties blocker, scalability is very high. Interceptor chain is starting from a custom interceptor, and then to the default of five interceptors. Under normal circumstances we want to print a network request log, so you can customize the Log interceptors, If you give all requests to add Header, Header can also customize the interceptor.

5 default interceptor meanings:

1. failure retry, redirector interceptor.
2. Bridge interceptor: add and remove some of the main header
3. Cache interceptor: According caching strategy, if the cache is available, simply returns the cached data.
4. Connection Pooling interceptor: http link connection pool caches, the benefits of connection pooling is multiplexed connections, fewer 3-way handshake, the request will be faster
5. interceptor real access to the network: internal use okio to send request

OKIO :( timeout, cache mechanism)

  • Sink: Similar java output stream OutputStream
  • Source: InputStream input stream of similar java
  • Buffer: At the name is a buffer, see the Buffer is not remembered when we usually write io stream will create a byte [] as a buffer, writing the data in this buffer is written to the stream, read when taking the data stream read into the buffer. Look down inside will know Buffer is actually a byte array.
  • ByteString: As the name suggests, this class with the byte and String relevant.
  • Segment implementation is a cycle lane list , when a Segment is full, the end of the chain to add a new Segment on and can store more data a. ** Buffer writes, in fact, a growing process Segment of the read operation, data is continuously consumed in the Segment. ** is to facilitate the transfer of data. When a Source of data to be read in a Sink, in fact, the data transferred from Source to Sink in the Buffer Buffer in. At this point if Buffer is a byte [] to achieve, then we certainly need to work byte array copy, but with the case of a linked list implementation, we can direct the pointer Segment node Sink in the Segment refers to the Source end of the list on OK, the array without performing copying more efficiently.
  • Caching mechanism is mainly managed by SegmentPool
Published 57 original articles · won praise 3 · Views 6208

Guess you like

Origin blog.csdn.net/qq_39830579/article/details/101760702