Overview

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:

What You Will Create

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.

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

Required Knowledge

To integrate hQUIC Kit, you must complete the following preparations:

Adding Build Dependencies

  1. Open the build.gradle file in the root directory of your Android Studio project.
  2. Add the Huawei Maven repository.
    • Go to buildscript > repositories and configure the Maven repository address for the HMS Core SDK.
    • Go to allprojects > repositories and configure the Maven repository address for the HMS Core SDK.
    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() ...... } }
  3. Open the build.gradle file in the app directory of your project.
  4. Add build dependencies in the dependencies block.
    dependencies { implementation 'com.huawei.hms:hquic-provider:{version}' }

Synchronizing the Project

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.

Creating a Service

Right-click the src directory in the app directory of your project, and choose related shortcut menus to create an HQUICService class.

Implementing Asynchronous Initialization

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"); } }); }

Creating a Cronet Engine

/** * 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; }

Constructing a Request

/** * 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; }

Sending the 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(); } }

Building, Loading, and Debugging the App

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:

Download

已复制代码