Gửi một yêu cầu đơn giản

Tìm hiểu cách sử dụng Thư viện Cronet để thực hiện các hoạt động mạng trong ứng dụng Android. Cronet là ngăn xếp mạng Chromium được cung cấp dưới dạng thư viện để bạn sử dụng trong các ứng dụng của mình. Để biết thêm thông tin về các tính năng của thư viện, hãy xem bài viết Thực hiện hoạt động mạng bằng Cronet.

Thiết lập thư viện trong dự án

Làm theo các bước sau để thêm phần phụ thuộc vào Thư viện Cronet trong dự án của bạn:

  1. Xác minh rằng Android Studio đã đưa tệp tham chiếu đến Kho lưu trữ Maven của Google vào tệp settings.gradle của dự án, như trong ví dụ sau:

    Groovy

    dependencyResolutionManagement {
       ...
       repositories {
           ...
           google()
       }
    }
    

    Kotlin

    dependencyResolutionManagement {
       ...
       repositories {
           ...
           google()
       }
    }
    
  2. Đưa nội dung tham chiếu đến Thư viện ứng dụng Dịch vụ Google Play cho Cronet trong phần dependencies của tệp build.gradle của mô-đun ứng dụng, như minh họa trong ví dụ sau:

    Groovy

    dependencies {
       implementation 'com.google.android.gms:play-services-cronet:18.0.1'
    }
    

    Kotlin

    dependencies {
       implementation("com.google.android.gms:play-services-cronet:18.0.1")
    }
    

Các đối tượng CronetEngine được tạo sau khi thêm phần phụ thuộc này sẽ sử dụng Cronet được tải từ Dịch vụ Google Play. Hãy gọi CronetProviderInstaller.installProvider(Context) trước khi tạo đối tượng CronetEngine để ngăn các ngoại lệ không mong muốn được gửi trong quá trình tạo CronetEngine do các lỗi như các thiết bị yêu cầu phiên bản cập nhật của Dịch vụ Google Play.

Trong trường hợp không thể tải Cronet từ các dịch vụ Google Play, các hoạt động triển khai API của Cronet có thể được sử dụng kém hiệu quả hơn. Để sử dụng phương thức triển khai dự phòng này, hãy phụ thuộc vào org.chromium.net:cronet-fallback và gọi new JavaCronetProvider(context).createBuilder().

Tạo một yêu cầu mạng

Phần này cho biết cách tạo và gửi một yêu cầu mạng bằng Thư viện Cronet. Sau khi gửi yêu cầu mạng, ứng dụng của bạn nên xử lý phản hồi mạng.

Tạo và định cấu hình một thực thể của CronetEngine

Thư viện này cung cấp một lớp CronetEngine.Builder mà bạn có thể dùng để tạo một thực thể của CronetEngine. Ví dụ sau cho thấy cách tạo đối tượng CronetEngine:

Kotlin

val myBuilder = CronetEngine.Builder(context)
val cronetEngine: CronetEngine = myBuilder.build()

Java

CronetEngine.Builder myBuilder = new CronetEngine.Builder(context);
CronetEngine cronetEngine = myBuilder.build();

Bạn có thể sử dụng lớp Builder để định cấu hình đối tượng CronetEngine, ví dụ: bạn có thể cung cấp các tuỳ chọn như lưu vào bộ nhớ đệm và nén dữ liệu. Để biết thêm thông tin, hãy xem CronetEngine.Builder.

Cung cấp phương thức triển khai lệnh gọi lại yêu cầu

Để cung cấp phương thức triển khai lệnh gọi lại, hãy tạo một lớp con của UrlRequest.Callback và triển khai các phương thức trừu tượng bắt buộc, như trong ví dụ sau:

Kotlin

private const val TAG = "MyUrlRequestCallback"

class MyUrlRequestCallback : UrlRequest.Callback() {
    override fun onRedirectReceived(request: UrlRequest?, info: UrlResponseInfo?, newLocationUrl: String?) {
        Log.i(TAG, "onRedirectReceived method called.")
        // You should call the request.followRedirect() method to continue
        // processing the request.
        request?.followRedirect()
    }

    override fun onResponseStarted(request: UrlRequest?, info: UrlResponseInfo?) {
        Log.i(TAG, "onResponseStarted method called.")
        // You should call the request.read() method before the request can be
        // further processed. The following instruction provides a ByteBuffer object
        // with a capacity of 102400 bytes for the read() method. The same buffer
        // with data is passed to the onReadCompleted() method.
        request?.read(ByteBuffer.allocateDirect(102400))
    }

    override fun onReadCompleted(request: UrlRequest?, info: UrlResponseInfo?, byteBuffer: ByteBuffer?) {
        Log.i(TAG, "onReadCompleted method called.")
        // You should keep reading the request until there's no more data.
        byteBuffer.clear()
        request?.read(byteBuffer)
    }

    override fun onSucceeded(request: UrlRequest?, info: UrlResponseInfo?) {
        Log.i(TAG, "onSucceeded method called.")
    }
}

Java

class MyUrlRequestCallback extends UrlRequest.Callback {
  private static final String TAG = "MyUrlRequestCallback";

  @Override
  public void onRedirectReceived(UrlRequest request, UrlResponseInfo info, String newLocationUrl) {
    Log.i(TAG, "onRedirectReceived method called.");
    // You should call the request.followRedirect() method to continue
    // processing the request.
    request.followRedirect();
  }

  @Override
  public void onResponseStarted(UrlRequest request, UrlResponseInfo info) {
    Log.i(TAG, "onResponseStarted method called.");
    // You should call the request.read() method before the request can be
    // further processed. The following instruction provides a ByteBuffer object
    // with a capacity of 102400 bytes for the read() method. The same buffer
    // with data is passed to the onReadCompleted() method.
    request.read(ByteBuffer.allocateDirect(102400));
  }

  @Override
  public void onReadCompleted(UrlRequest request, UrlResponseInfo info, ByteBuffer byteBuffer) {
    Log.i(TAG, "onReadCompleted method called.");
    // You should keep reading the request until there's no more data.
    byteBuffer.clear();
    request.read(byteBuffer);
  }

  @Override
  public void onSucceeded(UrlRequest request, UrlResponseInfo info) {
    Log.i(TAG, "onSucceeded method called.");
  }
}

Tạo đối tượng Executor để quản lý các nhiệm vụ mạng

Bạn có thể sử dụng lớp Executor để thực thi các tác vụ mạng. Để nhận thực thể của Executor, hãy sử dụng một trong các phương thức tĩnh của lớp Executors để trả về đối tượng Executor. Ví dụ sau đây cho biết cách tạo đối tượng Executor bằng phương thức newSingleThreadExecutor():

Kotlin

val executor: Executor = Executors.newSingleThreadExecutor()

Java

Executor executor = Executors.newSingleThreadExecutor();

Tạo và định cấu hình đối tượng UrlRequest

Để tạo yêu cầu mạng, hãy gọi phương thức newUrlRequestBuilder() của CronetEngine truyền URL đích, một thực thể của lớp gọi lại và đối tượng thực thi. Phương thức newUrlRequestBuilder() trả về một đối tượng UrlRequest.Builder mà bạn có thể dùng để tạo đối tượng UrlRequest, như trong ví dụ sau:

Kotlin

val requestBuilder = cronetEngine.newUrlRequestBuilder(
        "https://www.example.com",
        MyUrlRequestCallback(),
        executor
)

val request: UrlRequest = requestBuilder.build()

Java

UrlRequest.Builder requestBuilder = cronetEngine.newUrlRequestBuilder(
        "https://www.example.com", new MyUrlRequestCallback(), executor);

UrlRequest request = requestBuilder.build();

Bạn có thể dùng lớp Builder để định cấu hình thực thể của UrlRequest. Ví dụ: bạn có thể chỉ định mức độ ưu tiên hoặc động từ HTTP. Để biết thêm thông tin, hãy xem UrlRequest.Builder.

Để bắt đầu tác vụ mạng, hãy gọi phương thức start() của yêu cầu:

Kotlin

request.start()

Java

request.start();

Bằng cách làm theo hướng dẫn trong phần này, bạn có thể tạo và gửi yêu cầu mạng bằng Cronet. Tuy nhiên, để đơn giản, quy trình triển khai mẫu của UrlRequest.Callback chỉ in thông báo vào nhật ký. Phần sau đây cho biết cách cung cấp phương thức triển khai lệnh gọi lại hỗ trợ nhiều trường hợp hữu ích hơn, chẳng hạn như trích xuất dữ liệu từ phản hồi và phát hiện lỗi trong yêu cầu.

Xử lý phản hồi mạng

Sau khi bạn gọi phương thức start(), vòng đời yêu cầu Cronet sẽ bắt đầu. Ứng dụng của bạn nên quản lý yêu cầu trong vòng đời bằng cách chỉ định một lệnh gọi lại. Để tìm hiểu thêm về vòng đời, hãy xem phần Vòng đời yêu cầu Cronet. Bạn có thể chỉ định lệnh gọi lại bằng cách tạo một lớp con của UrlRequest.Callback và triển khai các phương thức sau:

onRedirectReceived()

Được gọi khi máy chủ gửi mã chuyển hướng HTTP để phản hồi yêu cầu ban đầu. Để theo lệnh chuyển hướng tới đích đến mới, hãy sử dụng phương thức followRedirect(). Nếu không, hãy sử dụng phương thức cancel(). Ví dụ sau đây cho thấy cách triển khai phương thức này:

Kotlin

override fun onRedirectReceived(request: UrlRequest?, info: UrlResponseInfo?, newLocationUrl: String?) {
  // Determine whether you want to follow the redirect.
  ...

  if (shouldFollow) {
      request?.followRedirect()
  } else {
      request?.cancel()
  }
}

Java

@Override
public void onRedirectReceived(UrlRequest request, UrlResponseInfo info, String newLocationUrl) {
  // Determine whether you want to follow the redirect.
  …

  if (shouldFollow) {
    request.followRedirect();
  } else {
    request.cancel();
  }
}
onResponseStarted()

Được gọi khi nhận được bộ tiêu đề cuối cùng. Phương thức onResponseStarted() chỉ được gọi sau khi tuân thủ tất cả các lệnh chuyển hướng. Mã sau đây cho thấy ví dụ về cách triển khai phương thức này:

Kotlin

override fun onResponseStarted(request: UrlRequest?, info: UrlResponseInfo?) {
  val httpStatusCode = info?.httpStatusCode
  if (httpStatusCode == 200) {
    // The request was fulfilled. Start reading the response.
    request?.read(myBuffer)
  } else if (httpStatusCode == 503) {
    // The service is unavailable. You should still check if the request
    // contains some data.
    request?.read(myBuffer)
  }
  responseHeaders = info?.allHeaders
}

Java

@Override
public void onResponseStarted(UrlRequest request, UrlResponseInfo info) {
  int httpStatusCode = info.getHttpStatusCode();
  if (httpStatusCode == 200) {
    // The request was fulfilled. Start reading the response.
    request.read(myBuffer);
  } else if (httpStatusCode == 503) {
    // The service is unavailable. You should still check if the request
    // contains some data.
    request.read(myBuffer);
  }
  responseHeaders = info.getAllHeaders();
}
onReadCompleted()

Được gọi bất cứ khi nào một phần của nội dung phản hồi được đọc. Ví dụ về mã sau đây cho biết cách triển khai phương thức và trích xuất nội dung phản hồi:

Kotlin

override fun onReadCompleted(request: UrlRequest?, info: UrlResponseInfo?, byteBuffer: ByteBuffer?) {
  // The response body is available, process byteBuffer.
  ...

  // Continue reading the response body by reusing the same buffer
  // until the response has been completed.
  byteBuffer?.clear()
  request?.read(myBuffer)
}

Java

@Override
public void onReadCompleted(UrlRequest request, UrlResponseInfo info, ByteBuffer byteBuffer) {
  // The response body is available, process byteBuffer.
  …

  // Continue reading the response body by reusing the same buffer
  // until the response has been completed.
  byteBuffer.clear();
  request.read(myBuffer);
}
onSucceeded()

Được gọi khi yêu cầu mạng hoàn tất thành công. Ví dụ sau đây cho thấy cách triển khai phương thức này:

Kotlin

override fun onSucceeded(request: UrlRequest?, info: UrlResponseInfo?) {
    // The request has completed successfully.
}

Java

@Override
public void onSucceeded(UrlRequest request, UrlResponseInfo info) {
  // The request has completed successfully.
}
onFailed()

Được gọi nếu yêu cầu không thành công vì bất kỳ lý do nào sau khi phương thức start() được gọi. Ví dụ sau đây cho thấy cách triển khai phương thức và nhận thông tin về lỗi:

Kotlin

override fun onFailed(request: UrlRequest?, info: UrlResponseInfo?, error: CronetException?) {
    // The request has failed. If possible, handle the error.
    Log.e(TAG, "The request failed.", error)
}

Java

@Override
public void onFailed(UrlRequest request, UrlResponseInfo info, CronetException error) {
  // The request has failed. If possible, handle the error.
  Log.e(TAG, "The request failed.", error);
}
onCanceled()

Được gọi nếu yêu cầu bị huỷ bằng phương thức cancel(). Sau khi được gọi, sẽ không có phương thức nào khác của lớp UrlRequest.Callback được gọi. Bạn có thể sử dụng phương thức này để giải phóng tài nguyên được phân bổ nhằm xử lý yêu cầu. Ví dụ sau đây cho thấy cách triển khai phương thức này:

Kotlin

override fun onCanceled(request: UrlRequest?, info: UrlResponseInfo?) {
    // Free resources allocated to process this request.
    ...
}

Java

@Override
public void onCanceled(UrlRequest request, UrlResponseInfo info) {
  // Free resources allocated to process this request.
  …
}