| OLD | NEW |
| 1 # Quick Start Guide to Using Cronet | 1 # Quick Start Guide to Using Cronet |
| 2 Cronet is the networking stack of Chromium put into a library for use on | 2 Cronet is the networking stack of Chromium put into a library for use on |
| 3 mobile. This is the same networking stack that is used in the Chrome browser | 3 mobile. This is the same networking stack that is used in the Chrome browser |
| 4 by over a billion people. It offers an easy-to-use, high performance, | 4 by over a billion people. It offers an easy-to-use, high performance, |
| 5 standards-compliant, and secure way to perform HTTP requests. Cronet has support | 5 standards-compliant, and secure way to perform HTTP requests. Cronet has support |
| 6 for both Android and iOS. On Android, Cronet offers its own Java asynchronous | 6 for both Android and iOS. On Android, Cronet offers its own Java asynchronous |
| 7 API as well as support for the [java.net.HttpURLConnection] API. | 7 API as well as support for the [java.net.HttpURLConnection] API. |
| 8 This document gives a brief introduction to using these two Java APIs. | 8 This document gives a brief introduction to using these two Java APIs. |
| 9 | 9 |
| 10 ### Basics | 10 ### Basics |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 ResponseInfo responseInfo, UrlRequestException error) { | 61 ResponseInfo responseInfo, UrlRequestException error) { |
| 62 // Request has failed. responseInfo might be null. | 62 // Request has failed. responseInfo might be null. |
| 63 Log.e("MyListener", "Request failed. " + error.getMessage()); | 63 Log.e("MyListener", "Request failed. " + error.getMessage()); |
| 64 // Maybe handle error here. Typical errors include hostname | 64 // Maybe handle error here. Typical errors include hostname |
| 65 // not resolved, connection to server refused, etc. | 65 // not resolved, connection to server refused, etc. |
| 66 } | 66 } |
| 67 } | 67 } |
| 68 | 68 |
| 69 Make a request like this: | 69 Make a request like this: |
| 70 | 70 |
| 71 UrlRequestContextConfig myConfig = new UrlRequestContextConfig(); | 71 CronetEngine.Builder engineBuilder = new CronetEngine.Builder(getContext()); |
| 72 CronetUrlRequestContext myRequestContext = | 72 CronetEngine engine = engineBuilder.build(); |
| 73 new CronetUrlRequestContext(getContext(), myConfig); | |
| 74 Executor executor = Executors.newSingleThreadExecutor(); | 73 Executor executor = Executors.newSingleThreadExecutor(); |
| 75 MyListener listener = new MyListener(); | 74 MyListener listener = new MyListener(); |
| 76 UrlRequest request = myRequestContext.createRequest( | 75 UrlRequest.Builder requestBuilder = new UrlRequest.Builder( |
| 77 "https://www.example.com", listener, executor); | 76 "https://www.example.com", listener, executor, engine); |
| 77 UrlRequest request = requestBuilder.build(); |
| 78 request.start(); | 78 request.start(); |
| 79 | 79 |
| 80 In the above example, `MyListener` extends `UrlRequestListener`. The request | 80 In the above example, `MyListener` extends `UrlRequestListener`. The request |
| 81 is started asynchronously. When the response is ready (fully or partially), and | 81 is started asynchronously. When the response is ready (fully or partially), and |
| 82 in the event of failures or redirects, `listener`'s methods will be invoked on | 82 in the event of failures or redirects, `listener`'s methods will be invoked on |
| 83 `executor`'s thread to inform the client of the request state and/or response | 83 `executor`'s thread to inform the client of the request state and/or response |
| 84 information. | 84 information. |
| 85 | 85 |
| 86 ### Downloading Data | 86 ### Downloading Data |
| 87 When Cronet fetches response headers from the server or gets them from the | 87 When Cronet fetches response headers from the server or gets them from the |
| 88 cache, `UrlRequestListener.onResponseStarted` will be invoked. To read the | 88 cache, `UrlRequestListener.onResponseStarted` will be invoked. To read the |
| 89 response body, the client should call `UrlRequest.read` and supply a | 89 response body, the client should call `UrlRequest.read` and supply a |
| 90 [ByteBuffer] for Cronet to fill. Once a portion or all of | 90 [ByteBuffer] for Cronet to fill. Once a portion or all of |
| 91 the response body is read, `UrlRequestListener.onReadCompleted` will be invoked. | 91 the response body is read, `UrlRequestListener.onReadCompleted` will be invoked. |
| 92 The client may then read and consume the data within `byteBuffer`. | 92 The client may then read and consume the data within `byteBuffer`. |
| 93 Once the client is ready to consume more data, the client should call | 93 Once the client is ready to consume more data, the client should call |
| 94 `UrlRequest.read` again. The process continues until | 94 `UrlRequest.read` again. The process continues until |
| 95 `UrlRequestListener.onSucceeded` or `UrlRequestListener.onFailed` is invoked, | 95 `UrlRequestListener.onSucceeded` or `UrlRequestListener.onFailed` is invoked, |
| 96 which signals the completion of the request. | 96 which signals the completion of the request. |
| 97 | 97 |
| 98 ### Uploading Data | 98 ### Uploading Data |
| 99 MyUploadDataProvider myUploadDataProvider = new MyUploadDataProvider(); | 99 MyUploadDataProvider myUploadDataProvider = new MyUploadDataProvider(); |
| 100 request.setHttpMethod("POST"); | 100 requestBuilder.setHttpMethod("POST"); |
| 101 request.setUploadDataProvider(myUploadDataProvider, executor); | 101 requestBuilder.setUploadDataProvider(myUploadDataProvider, executor); |
| 102 request.start(); | |
| 103 | 102 |
| 104 In the above example, `MyUploadDataProvider` extends `UploadDataProvider`. | 103 In the above example, `MyUploadDataProvider` extends `UploadDataProvider`. |
| 105 When Cronet is ready to send the request body, | 104 When Cronet is ready to send the request body, |
| 106 `myUploadDataProvider.read(UploadDataSink uploadDataSink, | 105 `myUploadDataProvider.read(UploadDataSink uploadDataSink, |
| 107 ByteBuffer byteBuffer)` will be invoked. The client will need to write the | 106 ByteBuffer byteBuffer)` will be invoked. The client will need to write the |
| 108 request body into `byteBuffer`. Once the client is done writing into | 107 request body into `byteBuffer`. Once the client is done writing into |
| 109 `byteBuffer`, the client can let Cronet know by calling | 108 `byteBuffer`, the client can let Cronet know by calling |
| 110 `uploadDataSink.onReadSucceeded`. If the request body doesn't fit into | 109 `uploadDataSink.onReadSucceeded`. If the request body doesn't fit into |
| 111 `byteBuffer`, the client can continue writing when `UploadDataProvider.read` is | 110 `byteBuffer`, the client can continue writing when `UploadDataProvider.read` is |
| 112 invoked again. For more details, please see the API reference. | 111 invoked again. For more details, please see the API reference. |
| 113 | 112 |
| 114 ### <a id=configuring-cronet></a> Configuring Cronet | 113 ### <a id=configuring-cronet></a> Configuring Cronet |
| 115 Various configuration options are available via the `UrlRequestContextConfig` | 114 Various configuration options are available via the `CronetEngine.Builder` |
| 116 object. | 115 object. |
| 117 | 116 |
| 118 Enabling HTTP/2, QUIC, or SDCH: | 117 Enabling HTTP/2, QUIC, or SDCH: |
| 119 | 118 |
| 120 - For Example: | 119 - For Example: |
| 121 | 120 |
| 122 myConfig.enableSPDY(true).enableQUIC(true).enableSDCH(true); | 121 engineBuilder.enableSPDY(true).enableQUIC(true).enableSDCH(true); |
| 123 | 122 |
| 124 Controlling the cache: | 123 Controlling the cache: |
| 125 | 124 |
| 126 - Use a 100KiB in-memory cache: | 125 - Use a 100KiB in-memory cache: |
| 127 | 126 |
| 128 myConfig.enableHttpCache( | 127 engineBuilder.enableHttpCache( |
| 129 UrlRequestContextConfig.HttpCache.IN_MEMORY, 100 * 1024); | 128 CronetEngine.Builder.HttpCache.IN_MEMORY, 100 * 1024); |
| 130 | 129 |
| 131 - or use a 1MiB disk cache: | 130 - or use a 1MiB disk cache: |
| 132 | 131 |
| 133 myConfig.setStoragePath(storagePathString); | 132 engineBuilder.setStoragePath(storagePathString); |
| 134 myConfig.enableHttpCache(UrlRequestContextConfig.HttpCache.DISK, | 133 engineBuilder.enableHttpCache(CronetEngine.Builder.HttpCache.DISK, |
| 135 1024 * 1024); | 134 1024 * 1024); |
| 136 | 135 |
| 137 ### Debugging | 136 ### Debugging |
| 138 To get more information about how Cronet is processing network | 137 To get more information about how Cronet is processing network |
| 139 requests, you can start and stop **NetLog** logging by calling | 138 requests, you can start and stop **NetLog** logging by calling |
| 140 `UrlRequestContext.startNetLogToFile` and `UrlRequestContext.stopNetLog`. | 139 `CronetEngine.startNetLogToFile` and `CronetEngine.stopNetLog`. |
| 141 Bear in mind that logs may contain sensitive data. You may analyze the | 140 Bear in mind that logs may contain sensitive data. You may analyze the |
| 142 generated log by navigating to [chrome://net-internals#import] using a | 141 generated log by navigating to [chrome://net-internals#import] using a |
| 143 Chrome browser. | 142 Chrome browser. |
| 144 | 143 |
| 145 # Using the java.net.HttpURLConnection API | 144 # Using the java.net.HttpURLConnection API |
| 146 Cronet offers an implementation of the [java.net.HttpURLConnection] API to make | 145 Cronet offers an implementation of the [java.net.HttpURLConnection] API to make |
| 147 it easier for apps which rely on this API to use Cronet. | 146 it easier for apps which rely on this API to use Cronet. |
| 148 To use Cronet's implementation instead of the system's default implementation, | 147 To use Cronet's implementation instead of the system's default implementation, |
| 149 simply do the following: | 148 simply do the following: |
| 150 | 149 |
| 151 CronetURLStreamHandlerFactory streamHandlerFactory = | 150 CronetURLStreamHandlerFactory streamHandlerFactory = |
| 152 new CronetURLStreamHandlerFactory(getContext(), myConfig); | 151 new CronetURLStreamHandlerFactory(engine); |
| 153 URL.setURLStreamHandlerFactory(streamHandlerFactory); | 152 URL.setURLStreamHandlerFactory(streamHandlerFactory); |
| 154 | 153 |
| 155 Cronet's | 154 Cronet's |
| 156 HttpURLConnection implementation has some limitations as compared to the system | 155 HttpURLConnection implementation has some limitations as compared to the system |
| 157 implementation, including not utilizing the default system HTTP cache (Please | 156 implementation, including not utilizing the default system HTTP cache (Please |
| 158 see {@link org.chromium.net.urlconnection.CronetURLStreamHandlerFactory} for | 157 see {@link org.chromium.net.urlconnection.CronetURLStreamHandlerFactory} for |
| 159 more information). | 158 more information). |
| 160 You can configure Cronet and control caching through the | 159 You can configure Cronet and control caching through the |
| 161 `UrlRequestContextConfig` instance, `myConfig` | 160 `CronetEngine.Builder` instance, `engineBuilder` |
| 162 (See [Configuring Cronet](#configuring-cronet) section), before you pass it | 161 (See [Configuring Cronet](#configuring-cronet) section), before you pass it |
| 163 into the `CronetURLStreamHandlerFactory` constructor. | 162 into the `CronetURLStreamHandlerFactory` constructor. |
| 164 | 163 |
| 165 [ByteBuffer]: https://developer.android.com/reference/java/nio/ByteBuffer.html | 164 [ByteBuffer]: https://developer.android.com/reference/java/nio/ByteBuffer.html |
| 166 [chrome://net-internals#import]: chrome://net-internals#import | 165 [chrome://net-internals#import]: chrome://net-internals#import |
| 167 [java.net.HttpURLConnection]: https://developer.android.com/reference/java/net/H
ttpURLConnection.html | 166 [java.net.HttpURLConnection]: https://developer.android.com/reference/java/net/H
ttpURLConnection.html |
| OLD | NEW |