Mar Teen
Mar Teen

Reputation: 117

Java 1.8 URLDecoder alternatives

I have the following code which make URL/post body decode:

String getUrl = java.net.URLDecoder.decode(getUrl, StandardCharsets.UTF_8);
String postData = java.net.URLDecoder.decode(postData, StandardCharsets.UTF_8);

When I'm trying to run it (I must use Java 1.8) I got the following:

>C:\Program Files\Java\jdk1.8.0_231\bin\java.exe" -jar myjar.jar GET https://my.url/path
Exception in thread "main" java.lang.NoSuchMethodError: java.net.URLDecoder.decode(Ljava/lang/String;Ljava/nio/charset/Charset;)Ljava/lang/String;

What is my alternative to decode URL (with same charsets - UTF8) using Jdk 1.8 (Unable to upgrade)?

Thanks!

Upvotes: 1

Views: 2045

Answers (1)

Ondra K.
Ondra K.

Reputation: 3077

The documentation of UrlDecoder#decode indicates that the signature of the method is (String, String) -> String (both arguments are Strings), while you're passing String and Charset. You can change the second parameter to String for instance by calling Charset#name:

String getUrl = java.net.URLDecoder.decode(getUrl, StandardCharsets.UTF_8.name());

Upvotes: 3

Related Questions