tree: ba841e03b696502547eb77571faba3494c741cc9 [path history] [tgz]
  1. android/
  2. ios/
  3. libcurl_on_cronet/
  4. native/
  5. test/
  6. third_party/
  7. tools/
  11. cronet_global_state.h
  14. cronet_prefs_manager.h
  16. cronet_upload_data_stream.h
  18. cronet_url_request.h
  20. cronet_url_request_context.h
  21. DEPS
  23. histogram_manager.h
  26. host_cache_persistence_manager.h
  29. metrics_util.h
  30. OWNERS
  32. pylintrc
  36. stale_host_resolver.h
  40. url_request_context_config.h

Quick Start Guide to Using Cronet

Cronet is the networking stack of Chromium put into a library for use on mobile. This is the same networking stack that is used in the Chrome browser by over a billion people. It offers an easy-to-use, high performance, standards-compliant, and secure way to perform HTTP requests. Cronet has support for both Android and iOS. On Android, Cronet offers its own Java asynchronous API as well as support for the API. This document gives a brief introduction to using these two Java APIs.

For instructions on checking out and building Cronet see Cronet build instructions.

Testing information is available on the native API and Android API pages.


First you will need to extend UrlRequest.Callback to handle events during the lifetime of a request. For example:

class MyCallback extends UrlRequest.Callback {
    public void onRedirectReceived(UrlRequest request,
            UrlResponseInfo responseInfo, String newLocationUrl) {
        if (followRedirect) {
            // Let's tell Cronet to follow the redirect!
        } else {
            // Not worth following the redirect? Abandon the request.

    public void onResponseStarted(UrlRequest request,
            UrlResponseInfo responseInfo) {
         // Now we have response headers!
         int httpStatusCode = responseInfo.getHttpStatusCode();
         if (httpStatusCode == 200) {
             // Success! Let's tell Cronet to read the response body.
         } else if (httpStatusCode == 503) {
             // Do something. Note that 4XX and 5XX are not considered
             // errors from Cronet's perspective since the response is
             // successfully read.
         mResponseHeaders = responseInfo.getAllHeaders();

    public void onReadCompleted(UrlRequest request,
            UrlResponseInfo responseInfo, ByteBuffer byteBuffer) {
         // Response body is available.
         // Let's tell Cronet to continue reading the response body or
         // inform us that the response is complete!;

    public void onSucceeded(UrlRequest request,
            UrlResponseInfo responseInfo) {
         // Request has completed successfully!

    public void onFailed(UrlRequest request,
            UrlResponseInfo responseInfo, CronetException error) {
         // Request has failed. responseInfo might be null.
         Log.e("MyCallback", "Request failed. " + error.getMessage());
         // Maybe handle error here. Typical errors include hostname
         // not resolved, connection to server refused, etc.

Make a request like this:

CronetEngine.Builder engineBuilder = new CronetEngine.Builder(getContext());
CronetEngine engine =;
Executor executor = Executors.newSingleThreadExecutor();
MyCallback callback = new MyCallback();
UrlRequest.Builder requestBuilder = engine.newUrlRequestBuilder(
        "", callback, executor);
UrlRequest request =;

In the above example, MyCallback extends UrlRequest.Callback. The request is started asynchronously. When the response is ready (fully or partially), and in the event of failures or redirects, callback's methods will be invoked on executor's thread to inform the client of the request state and/or response information.

Downloading Data

When Cronet fetches response headers from the server or gets them from the cache, UrlRequest.Callback.onResponseStarted will be invoked. To read the response body, the client should call and supply a ByteBuffer for Cronet to fill. Once a portion or all of the response body is read, UrlRequest.Callback.onReadCompleted will be invoked. The client may then read and consume the data within byteBuffer. Once the client is ready to consume more data, the client should call again. The process continues until UrlRequest.Callback.onSucceeded or UrlRequest.Callback.onFailed is invoked, which signals the completion of the request.

Uploading Data

MyUploadDataProvider myUploadDataProvider = new MyUploadDataProvider();
requestBuilder.setUploadDataProvider(myUploadDataProvider, executor);

In the above example, MyUploadDataProvider extends UploadDataProvider. When Cronet is ready to send the request body, uploadDataSink, ByteBuffer byteBuffer) will be invoked. The client will need to write the request body into byteBuffer. Once the client is done writing into byteBuffer, the client can let Cronet know by calling uploadDataSink.onReadSucceeded. If the request body doesn't fit into byteBuffer, the client can continue writing when is invoked again. For more details, please see the API reference.

Configuring Cronet

Various configuration options are available via the CronetEngine.Builder object.

Enabling HTTP/2 and QUIC:

  • For Example:


Controlling the cache:

  • Use a 100KiB in-memory cache:

              CronetEngine.Builder.HttpCache.IN_MEMORY, 100 * 1024);
  • or use a 1MiB disk cache:

              1024 * 1024);


To get more information about how Cronet is processing network requests, you can start and stop NetLog logging by calling CronetEngine.startNetLogToFile and CronetEngine.stopNetLog. Bear in mind that logs may contain sensitive data. You may analyze the generated log by navigating to chrome://net-internals#import using a Chrome browser.

Using the API

Cronet offers an implementation of the API to make it easier for apps which rely on this API to use Cronet. To open individual connections using Cronet's implementation, do the following:

HttpURLConnection connection =

To use Cronet‘s implementation instead of the system’s default implementation for all connections established using URL.openConnection() do the following:


Cronet's HttpURLConnection implementation has some limitations as compared to the system implementation, including not utilizing the default system HTTP cache (Please see {@link} for more information). You can configure Cronet and control caching through the CronetEngine.Builder instance, engineBuilder (See Configuring Cronet section), before you build the CronetEngine and then call CronetEngine.createURLStreamHandlerFactory().