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 |
11 First you will need to extend `UrlRequestListener` to handle | 11 First you will need to extend `UrlRequestListener` to handle |
12 events during the lifetime of a request. For example: | 12 events during the lifetime of a request. For example: |
13 | 13 |
14 class MyListener extends UrlRequestListener { | 14 class MyListener extends UrlRequestListener { |
15 @Override | 15 @Override |
16 public void onReceivedRedirect(UrlRequest request, | 16 public void onReceivedRedirect(UrlRequest request, |
17 ResponseInfo responseInfo, String newLocationUrl) { | 17 UrlResponseInfo responseInfo, String newLocationUrl) { |
18 if (followRedirect) { | 18 if (followRedirect) { |
19 // Let's tell Cronet to follow the redirect! | 19 // Let's tell Cronet to follow the redirect! |
20 mRequest.followRedirect(); | 20 mRequest.followRedirect(); |
21 } else { | 21 } else { |
22 // Not worth following the redirect? Abandon the request. | 22 // Not worth following the redirect? Abandon the request. |
23 mRequest.cancel(); | 23 mRequest.cancel(); |
24 } | 24 } |
25 } | 25 } |
26 | 26 |
27 @Override | 27 @Override |
28 public void onResponseStarted(UrlRequest request, | 28 public void onResponseStarted(UrlRequest request, |
29 ResponseInfo responseInfo) { | 29 UrlResponseInfo responseInfo) { |
30 // Now we have response headers! | 30 // Now we have response headers! |
31 int httpStatusCode = responseInfo.getHttpStatusCode(); | 31 int httpStatusCode = responseInfo.getHttpStatusCode(); |
32 if (httpStatusCode == 200) { | 32 if (httpStatusCode == 200) { |
33 // Success! Let's tell Cronet to read the response body. | 33 // Success! Let's tell Cronet to read the response body. |
34 request.read(myBuffer); | 34 request.read(myBuffer); |
35 } else if (httpStatusCode == 503) { | 35 } else if (httpStatusCode == 503) { |
36 // Do something. Note that 4XX and 5XX are not considered | 36 // Do something. Note that 4XX and 5XX are not considered |
37 // errors from Cronet's perspective since the response is | 37 // errors from Cronet's perspective since the response is |
38 // successfully read. | 38 // successfully read. |
39 } | 39 } |
40 responseHeaders = responseInfo.getAllHeaders(); | 40 responseHeaders = responseInfo.getAllHeaders(); |
41 } | 41 } |
42 | 42 |
43 @Override | 43 @Override |
44 public void onReadCompleted(UrlRequest request, | 44 public void onReadCompleted(UrlRequest request, |
45 ResponseInfo responseInfo, ByteBuffer byteBuffer) { | 45 UrlResponseInfo responseInfo, ByteBuffer byteBuffer) { |
46 // Response body is available. | 46 // Response body is available. |
47 doSomethingWithResponseData(byteBuffer); | 47 doSomethingWithResponseData(byteBuffer); |
48 // Let's tell Cronet to continue reading the response body or | 48 // Let's tell Cronet to continue reading the response body or |
49 // inform us that the response is complete! | 49 // inform us that the response is complete! |
50 request.read(myBuffer); | 50 request.read(myBuffer); |
51 } | 51 } |
52 | 52 |
53 @Override | 53 @Override |
54 public void onSucceeded(UrlRequest request, | 54 public void onSucceeded(UrlRequest request, |
55 ExtendedResponseInfo extendedResponseInfo) { | 55 UrlResponseInfo responseInfo) { |
56 // Request has completed successfully! | 56 // Request has completed successfully! |
57 } | 57 } |
58 | 58 |
59 @Override | 59 @Override |
60 public void onFailed(UrlRequest request, | 60 public void onFailed(UrlRequest request, |
61 ResponseInfo responseInfo, UrlRequestException error) { | 61 UrlResponseInfo 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 CronetEngine.Builder engineBuilder = new CronetEngine.Builder(getContext()); | 71 CronetEngine.Builder engineBuilder = new CronetEngine.Builder(getContext()); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 see {@link org.chromium.net.CronetEngine#createURLStreamHandlerFactory} for | 160 see {@link org.chromium.net.CronetEngine#createURLStreamHandlerFactory} for |
161 more information). | 161 more information). |
162 You can configure Cronet and control caching through the | 162 You can configure Cronet and control caching through the |
163 `CronetEngine.Builder` instance, `engineBuilder` | 163 `CronetEngine.Builder` instance, `engineBuilder` |
164 (See [Configuring Cronet](#configuring-cronet) section), before you build the | 164 (See [Configuring Cronet](#configuring-cronet) section), before you build the |
165 `CronetEngine` and then call `CronetEngine.createURLStreamHandlerFactory()`. | 165 `CronetEngine` and then call `CronetEngine.createURLStreamHandlerFactory()`. |
166 | 166 |
167 [ByteBuffer]: https://developer.android.com/reference/java/nio/ByteBuffer.html | 167 [ByteBuffer]: https://developer.android.com/reference/java/nio/ByteBuffer.html |
168 [chrome://net-internals#import]: chrome://net-internals#import | 168 [chrome://net-internals#import]: chrome://net-internals#import |
169 [java.net.HttpURLConnection]: https://developer.android.com/reference/java/net/H
ttpURLConnection.html | 169 [java.net.HttpURLConnection]: https://developer.android.com/reference/java/net/H
ttpURLConnection.html |
OLD | NEW |