hQUIC Kit gives your apps low latency, high throughput, and secure and reliable communications capabilities. It supports the QUIC protocol and provides intelligent congestion control algorithms to avoid congestions in different network environments, giving your apps faster connection establishment, reduced packet loss, and higher throughput. You can use hQUIC Kit to improve the network communication quality in unstable mobile network environments. With the sample project and code, you will experience how to:
In this codelab, you will create an app, integrate hQUIC Kit into the app, enable Cronet, and use Cronet APIs to implement communications based on the HTTP/2 and QUIC protocols.
In this codelab, you will learn how to:
To integrate hQUIC Kit, you must complete the following preparations:
buildscript {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
google()
......
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
}
}
allprojects {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
google()
......
}
}
dependencies {
implementation 'com.huawei.hms:hquic-provider:{version}'
}
Go to File > Sync Project with Gradle Files to start synchronization.
If "completed successfully" is displayed, the synchronization is successful, as shown in the screenshot below.
In this section, you will develop a demo app, and learn how to use APIs of the hQUIC SDK.
Right-click the src directory in the app directory of your project, and choose related shortcut menus to create an HQUICService class.
Asynchronous initialization is optional. To load the Cronet service in advance, you can use the asynchronous initialization API to preload the service.
/**
* Asynchronously initialize HQUICManager.
*/
public void init(){
HQUICManager.asyncInit(context, new HQUICManager.HQUICInitCallback() {
@Override
public void onSuccess() {
Log.i(TAG, "HQUICManager asyncInit success");
}
@Override
public void onFail(Exception e) {
Log.w(TAG, "HQUICManager asyncInit fail");
}
});
}
/**
* Construct a CronetEngine object.
*/
private CronetEngine createCronetEngine(String url) {
if (cronetEngine != null) {
return cronetEngine;
}
CronetEngine.Builder builder = new CronetEngine.Builder(context);
// Set to support the QUIC protocol.
builder.enableQuic(true);
// Globally add domain names that support the QUIC protocol.
builder.addQuicHint(getHost(url), DEFAULT_PORT, DEFAULT_ALTERNATEPORT);
cronetEngine = builder.build();
return cronetEngine;
}
/**
* Construct a request object.
*/
private UrlRequest builRequest(String url, String method) {
CronetEngine cronetEngine = createCronetEngine(url);
UrlRequest.Builder requestBuilder =
cronetEngine.newUrlRequestBuilder(url, callback, executor).setHttpMethod(method);
UrlRequest request = requestBuilder.build();
return request;
}
/**
* Send the network request.
*/
private void sendRequest(String url, String method) {
Log.i(TAG, "callURL: url is " + url + "and method is " + method);
UrlRequest urlRequest = builRequest(url, method);
if (urlRequest != null) {
urlRequest.start();
}
}
After adding the preceding service code, you can build your app. After the app is built, an APK file will be generated, and you can install it on the test mobile phone.
Tap hQUICTest in the demo app. Relevant information will be displayed.
Well done. You have successfully completed this codelab and learned how to:
For more information, click the following links: