EdgeOne Logo
Documentation
请选择
请选择
Overview
Menu

Android

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();