tencent cloud

All product documents
Tencent Cloud EdgeOne
Last updated: 2023-06-29 11:20:56
Android
Last updated: 2023-06-29 11:20:56
The following code shows how to make QUIC requests with the Android client. For details of the API description, see Android APIs.

Creating GET Requests

//Create QuicClient and initialize QUIC configuration. It is recommended to use QuicClient as a global variable. For more information about the API, see %!s(<nil>).
QuicClient quicClient = new QuicClient.Builder()
.setCongestionType(QuicClient.CONGESTION_TYPE_BBR) //Use BBR algorithm.
.setConnectTimeoutMillis(6 * 1000) //Configure connection timeout.
.build();
//Create QuicRequest and specify the request URL.
String url="";
QuicRequest request = new QuicRequest.Builder(url).get().build();

//Execute the request asynchronously and get the result. See %!s(<nil>) for instructions.
quicClient.newCall(request).enqueue(new QuicCallback() {
@Override
public void onResponse(QuicCall call, QuicResponse response) throws IOException {
//When the request is executed successfully, it returns the response data.
ResponseBody body = response.body();
if(body != null) {
String res = body.string();
}
}
@Override
public void onFailed(QuicCall call, int errorCode, String error) {
//When the request fails to be executed, it returns the error message.
}
});

Creating POST Requests

//Create QuicClient and initialize QUIC configuration. It is recommended to use QuicClient as a global variable. For detailed steps, see %!s(<nil>).
QuicClient quicClient = new QuicClient.Builder()
.setCongestionType(QuicClient.CONGESTION_TYPE_BBR) //Use BBR algorithm.
.setConnectTimeoutMillis(3 * 1000) //Configure connection timeout.
.build();

//Construct body data.
String body="your body string";
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), body);

//Create QuicRequest.
String url="";
QuicRequest request = new QuicRequest.Builder(url).post(requestBody).build();

//Execute the request asynchronously and get the result. See %!s(<nil>) for instructions.
quicClient.newCall(request).enqueue(new QuicCallback() {
@Override
public void onResponse(QuicCall call, QuicResponse response) throws IOException {
//When the request is executed successfully, it returns the response data.
ResponseBody body = response.body();
if(body != null) {
String res = body.string();
}
}
@Override
public void onFailed(QuicCall call, int errorCode, String error) {
//When the request fails to be executed, it returns the error message.
}
});

Canceling Requests

...
//Create QuicCall. See %!s(<nil>) for instructions.
QuicCall quicCall = quicClient.newCall(request);

// Initiate a request.
...

//Cancel the request using the cancel method via QuicCall.
quicCall.cancel();

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon