首页 > 解决方案 > 如何获取 OkHttp 4.x 的 UserAgent?

问题描述

在 OkHttp 3.x 中,我们可以使用

import okhttp3.internal.Version

println(Version.userAgent())

但是,这在 OkHttp 4.x 中不再起作用。

在https://github.com/square/okhttp/issues/5969中提出了一个问题,并在https://github.com/square/okhttp/pull/5981中修复了,但我无法让它工作.

在更改日志中,声明从 4.7.0 版本开始。

New: Constant string okhttp3.VERSION. This is a string like “4.5.0-RC1”, “4.5.0”, or “4.6.0-SNAPSHOT” indicating the version of OkHttp in the current runtime. Use this to include the OkHttp version in custom User-Agent headers.

但我就是无法导入okhttp3.VERSION

标签: androidokhttp

解决方案


清除缓存并重新同步后,我现在可以拥有版本了

import okhttp3.OkHttp.VERSION

println(VERSION)

推荐阅读