如何使用带有 API 的用户代理字符串?

How to use an User Agent string with an API?

我正在使用 volley Android 库处理 API。

我必须将 Android 设备的用户代理作为参数传递给 API URL。

User Agent 字符串包含空格和一些 URL 中不允许的字符。

如何将用户代理字符串转换为可以与 API 一起使用的格式。

我正在使用以下解决方案来获取用户代理,但不知道如何进一步修改字符串以将其用于 API。

   private static String GetUserAgent(Context act) {
        String userAgent = new WebView(act).getSettings().getUserAgentString();

        return userAgent;
    }

您应该使用 URLEncoder

对其进行编码
 URLEncoder.encode(userAgent);

The space character " " is converted into a plus sign "+".