简体   繁体   English

Java中带有String.format的UnknownFormatConversionException

[英]UnknownFormatConversionException with String.format in Java

Why am I getting the error java.util.UnknownConversionException: Conversion = 'D' for the method below. 为什么我收到以下方法的错误java.util.UnknownConversionException:Conversion ='D'。 I create an intial string, then return that from a method to do a String.format. 我创建了一个初始字符串,然后从方法返回该字符串以执行String.format。 It happens at the String.format. 它发生在String.format。

String url = StringUtils.join(new String[] { BASE_URL,
                "/location/inst/", slug,
                "/coursesections/number%s.json?apiKey=", apiKey,
                "&token=", GetAuthTokenEncoded(authToken) })


String url2 = String.format(url, ("/" + callNumber));

Output of StringUtils StringUtils的输出

http://test.com/location/inst/testy/coursesections/number%s;.json?apiKey=dfsdfsdfsadfsdf&token=2.0%7Cidm%7Cidm%7Ctest%3Dbesrlin_50d4s9dd25dsee56f3a1e95fb4f%26berlin%3D50d49dd2e2f28cf450b0caa9f449%26campus%3Dberlin%3A50d49dd25ee56f3a1e95fb4f%7C2013-03-12T16%3A25%3A23%2B00%3A00%7C2013-03-12T19%3A25%3A23%2B00%3A00%7C09161a6011f2dc2e3443bc40b2d3f4b3d4f

GetAuthTokenEncoded(authToken) contains a whole load of % causing errors. GetAuthTokenEncoded(authToken)包含导致错误的%的整个负载。 Why not just append the callNumber in the same way you are joining the rest of the String? 为什么不以加入字符串其余部分的相同方式追加callNumber呢?

String url = StringUtils.join(new String[] { BASE_URL,
            "/location/inst/", part,
            "/coursesections/number/", number, ".json?apiKey=", apiKey,
            "&token=", GetAuthTokenEncoded(authToken) })

There is "%3D" in the url string to be formatted due to URL escaping. 由于URL转义, url字符串中存在要格式化的“%3D”。 You will need to supply the result of GetAuthTokenEncoded as a parameter to String.format so it doesn't interpret "%3D" as a placeholder. 您需要将GetAuthTokenEncoded的结果作为参数提供给String.format以便它不会将“%3D”解释为占位符。

String url = StringUtils.join(new String[] { BASE_URL,
            "/location/inst/", part,
            "/coursesections/number%s.json?apiKey=", apiKey,
            "&token=%s");
String url2 = String.format(url, ("/" + callNumber), GetAuthTokenEncoded(authToken));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM