2

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

 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这样结束

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

4

1 回答 1

2

“:”即将到来,因为您正在使用应该是(“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

于 2015-10-01T00:05:55.480 回答