URI Builder 结果中有一个意外的冒号(Android 编程)

URI Builder result has an unexpected colon in it (Android Programming)

对于 Uri.Builder,我正在使用 scheme(String) 并从那里构建一个 URL 字符串。但是,在我的最终 String 中有一个冒号符号 : 它会更改查询的结果。这是我的代码。

 Uri.Builder toBuild = new Uri.Builder();
        final String baseURL = "http://api.openweathermap.org/data/2.5/forecast/daily";

        toBuild.scheme(baseURL)
                .appendQueryParameter("zip", postal_Code[0])
                .appendQueryParameter("mode", "json")
                .appendQueryParameter("units", "metric")
                .appendQueryParameter("cnt", "7");

        String formURL = toBuild.toString();
        Log.v(LOG_TAG, "Formed URL: " + formURL);

我得到的字符串应该是 http://api.openweathermap.org/data/2.5/forecast/daily?zip=94043&mode=json&units=metric&cnt=7

但结尾却像 http://api.openweathermap.org/data/2.5/forecast/daily:?zip=94043&mode=json&units=metric&cnt=7

在基本 URL 字符串的每日之后出现冒号。请告知如何从字符串中删除冒号。谢谢

出现“:”是因为您正在使用应该是("http"、"https" 等)的方案设置 baseUrl,而在 url 方案中始终是后面跟着一个冒号,所以这就是为什么你会看到额外的冒号。

我会这样 URL 一部分一部分地构建这个:

    Uri.Builder builder = new Uri.Builder();
    builder.scheme("http")
            .authority("api.openweathermap.org")
            .appendPath("data")
            .appendPath("2.5")
            .appendPath("forecast")
            .appendPath("daily")
            .appendQueryParameter("zip", "94043")
            .appendQueryParameter("mode", "json")
            .appendQueryParameter("units", "metric")
            .appendQueryParameter("cnt", "7");
String formURL = builder.toString();

结果:http://api.openweathermap.org/data/2.5/forecast/daily?zip=94043&mode=json&units=metric&cnt=7