tree: 7b96e0e7b7dd3180885b9f8e6c4b975e2e2caf9b [path history] [tgz]
  1. DEPS
  5. android/
  6. cronet_static.gypi
  8. histogram_manager.h
  10. tools/
  12. url_request_context_config.h
  13. url_request_context_config_list.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.


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

class MyListener extends UrlRequestListener {
    public void onReceivedRedirect(UrlRequest request,
            ResponseInfo 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,
            ResponseInfo 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.
         responseHeaders = responseInfo.getAllHeaders();

    public void onReadCompleted(UrlRequest request,
            ResponseInfo 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,
            ExtendedResponseInfo extendedResponseInfo) {
         // Request has completed successfully!

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

Make a request like this:

UrlRequestContextConfig myConfig = new UrlRequestContextConfig();
CronetUrlRequestContext myRequestContext =
        new CronetUrlRequestContext(getContext(), myConfig);
Executor executor = Executors.newSingleThreadExecutor();
MyListener listener = new MyListener();
UrlRequest request = myRequestContext.createRequest(
        "", listener, executor);

In the above example, MyListener extends UrlRequestListener. The request is started asynchronously. When the response is ready (fully or partially), and in the event of failures or redirects, listener'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, UrlRequestListener.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, UrlRequestListener.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 UrlRequestListener.onSucceeded or UrlRequestListener.onFailed is invoked, which signals the completion of the request.

Uploading Data

MyUploadDataProvider myUploadDataProvider = new MyUploadDataProvider();
request.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 UrlRequestContextConfig object.

Enabling HTTP/2, QUIC, or SDCH:

  • For Example:


Controlling the cache:

  • Use a 100KiB in-memory cache:

              UrlRequestContextConfig.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 UrlRequestContext.startNetLogToFile and UrlRequestContext.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 use Cronet‘s implementation instead of the system’s default implementation, simply do the following:

CronetURLStreamHandlerFactory streamHandlerFactory =
        new CronetURLStreamHandlerFactory(getContext(), myConfig);

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 UrlRequestContextConfig instance, myConfig (See Configuring Cronet section), before you pass it into the CronetURLStreamHandlerFactory constructor.