Okhttp Source Code Analysis

Source: Internet
Author: User

http://frodoking.github.io/2015/03/12/android-okhttp/

# #总体设计

# #请求流程图

# #同步与异步的实现
When a request is initiated, the entire framework encapsulates each request primarily through call. At the same time call holds Okhttpclient and a copy of Httpengine. Each time a synchronous or asynchronous request will have dispatcher participation, the difference is:

  • Synchronous
    Dispatcher will record the currently executed task call in the synchronous execution queue, while executing the call Getresponsewithinterceptorchain () method in the current thread, and get the current return data response directly;
  • Asynchronous
    First of all, dispatcher,dispatcher. The internal implementation of lazy loading of the thread pool without boundary restrictions, and the thread pool used synchronousqueue such a blocking queue. Synchronousqueue Each insert operation must wait for the removal of another thread, and any removal operation waits for the other thread to insert the operation. So there is actually no element inside this queue, or capacity is 0, strictly speaking is not a container. The peek operation cannot be called because the queue has no capacity, because elements are only available when the element is removed. Obviously this is a fast way to pass elements, which means that in this case the element is always in the quickest way from the insert (producer) to the removal person (the consumer), which is the quickest way to process the task in a multi-tasking queue. For high-frequent requests, the scenario is undoubtedly the most suitable.
    Asynchronous execution is performed by Call.enqueue (Callback Responsecallback), adding an anonymous inner class Callback that encapsulates Runnable's call in dispatcher to perform the current call. Here's something to be aware of. This asynccall is the anonymous inner class of call. Asynccall's Execute method will still callback to call's Getresponsewithinterceptorchain method to complete the request, while returning the data or state through callback.

Okhttp Source Code Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.