Android

Last updated: 2023-09-07 15:47:51

The following code illustrates the creation of QUIC requests using the Android client. For a comprehensive understanding of the API, refer to the Android API Documentation.

Create a GET request.

//Instantiate QuicClient, initialize QUIC configuration. It is recommended to use it as a global variable after creation. For interface details, refer to the API Documentation.
QuicClient quicClient = new QuicClient.Builder()
.setCongestionType(QuicClient.CONGESTION_TYPE_BBR) //Utilize BBR congestion 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 retrieve the result. For usage instructions on QuicCall, refer to the API Documentation.
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.
}
});

Create a POST Request.

//Instantiate QuicClient, initialize QUIC configuration. It is recommended to use it as a global variable after creation. For detailed configuration instructions, refer to the API Documentation.
QuicClient quicClient = new QuicClient.Builder()
.setCongestionType(QuicClient.CONGESTION_TYPE_BBR) //Utilize BBR congestion 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 retrieve the result. For usage instructions on QuicCall, refer to the API Documentation.
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

...
//Instantiate QuicCall. For usage details, refer to the API Documentation.
QuicCall quicCall = quicClient.newCall(request);

// Initiate a request.
...

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