发送简单请求

了解如何使用 Cronet 库在您的 Android 应用中执行网络操作。Cronet 是 Chromium 网络堆栈,它以库的形式提供,供您在应用中使用。有关库功能的更多信息,请参阅 使用 Cronet 执行网络操作

在项目中设置库

按照以下步骤将 Cronet 库的依赖项添加到您的项目中

  1. 验证 Android Studio 是否在您的项目的 settings.gradle 文件中包含对 Google Maven 存储库的引用,如下例所示

    Groovy

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

    Kotlin

    dependencyResolutionManagement {
       ...
       repositories {
           ...
           google()
       }
    }
  2. 在应用模块的 build.gradle 文件的 dependencies 部分中包含对 Cronet 的 Google Play 服务客户端库的引用,如下例所示

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

添加此依赖项后创建的CronetEngine对象将使用从Google Play服务加载的Cronet。在创建CronetEngine对象之前,请调用CronetProviderInstaller.installProvider(Context),以防止由于设备需要更新版本的Google Play服务等错误而在创建CronetEngine期间抛出意外异常。

如果无法从Google Play服务加载Cronet,则可以使用性能较低的Cronet API实现。要使用此回退实现,请依赖org.chromium.net:cronet-fallback并调用new JavaCronetProvider(context).createBuilder()

创建网络请求

本节介绍如何使用Cronet库创建和发送网络请求。发送网络请求后,您的应用应处理网络响应

创建和配置CronetEngine实例

库提供了一个CronetEngine.Builder类,可用于创建CronetEngine实例。以下示例演示如何创建CronetEngine对象

Kotlin

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

Java

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

您可以使用Builder类配置CronetEngine对象,例如,您可以提供缓存和数据压缩等选项。有关更多信息,请参阅CronetEngine.Builder

提供请求回调的实现

要提供回调的实现,请创建UrlRequest.Callback的子类并实现必需的抽象方法,如下例所示

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

创建Executor对象以管理网络任务

您可以使用Executor类执行网络任务。要获取Executor的实例,请使用Executors类的返回Executor对象的静态方法之一。以下示例演示如何使用newSingleThreadExecutor()方法创建Executor对象

Kotlin

val executor: Executor = Executors.newSingleThreadExecutor()

Java

Executor executor = Executors.newSingleThreadExecutor();

创建和配置UrlRequest对象

要创建网络请求,请调用newUrlRequestBuilder()方法,该方法属于CronetEngine,并传递目标URL、回调类的实例和执行器对象。newUrlRequestBuilder()方法返回一个UrlRequest.Builder对象,可用于创建UrlRequest对象,如下例所示

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

您可以使用Builder类配置UrlRequest实例。例如,您可以指定优先级或HTTP动词。有关更多信息,请参阅UrlRequest.Builder

要启动网络任务,请调用请求的start()方法

Kotlin

request.start()

Java

request.start();

按照本节中的说明,您可以使用Cronet创建和发送网络请求。但是,为简单起见,UrlRequest.Callback的示例实现仅将消息打印到日志中。下一节介绍如何提供支持更有用场景(例如从响应中提取数据和检测请求失败)的回调实现。

处理网络响应

调用start()方法后,将启动Cronet请求生命周期。您的应用应通过指定回调来管理生命周期中的请求。要了解有关生命周期的更多信息,请参阅Cronet请求生命周期。您可以通过创建UrlRequest.Callback的子类并实现以下方法来指定回调

onRedirectReceived()

当服务器响应原始请求时发出HTTP重定向代码时调用。要遵循重定向到新目标,请使用followRedirect()方法。否则,请使用cancel()方法。以下示例演示如何实现该方法

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

收到最终标头集时调用。onResponseStarted()方法仅在遵循所有重定向后才调用。以下代码显示了该方法的示例实现

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

每当读取响应正文的一部分时调用。以下代码示例演示如何实现该方法并提取响应正文

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

网络请求成功完成时调用。以下示例演示如何实现该方法

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

如果在调用start()方法后由于任何原因请求失败,则调用。以下示例演示如何实现该方法并获取有关错误的信息

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

如果使用cancel()方法取消请求,则调用。调用后,不会调用UrlRequest.Callback类的任何其他方法。您可以使用此方法释放分配给处理请求的资源。以下示例演示如何实现该方法

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.
  
}