225

I get the following warning when using java.net.URLEncoder.encode:

warning: [deprecation] encode(java.lang.String)
         in java.net.URLEncoder has been deprecated

What should I be using instead?

Frank Krueger
  • 69,552
  • 46
  • 163
  • 208
  • 24
    This is answered in the deprecation tag in the docs: "Instead, use the encode(String,String) method to specify the encoding." See http://java.sun.com/javase/6/docs/api/java/net/URLEncoder.html. – Michael Myers Oct 17 '08 at 20:09

6 Answers6

311

Use the other encode method in URLEncoder:

URLEncoder.encode(String, String)

The first parameter is the text to encode; the second is the name of the character encoding to use (e.g., UTF-8). For example:

System.out.println(
  URLEncoder.encode(
    "urlParameterString",
    java.nio.charset.StandardCharsets.UTF_8.toString()
  )
);
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Will Wagner
  • 4,128
  • 3
  • 22
  • 14
  • 14
    @jsh: I'm confused, why shouldn't there be a URLDecoder? Why does this make Java bloated? These are static methods. It would take the same amount of effort to type either. If you like Python, why are you programming in Java? Is it because more people use Java than Python and you got a Java job instead of Python job? – stepanian Feb 27 '12 at 09:07
  • 2
    @stepanian yep, you gotta work where the work is. the python package i typically use puts all these encoding and decoding utilities under one roof. I mainly added this comment because I had to go and find the URLDecoder and I thought it might save somebody a couple of minutes to know this was what was required. – jsh Feb 29 '12 at 16:06
  • @jsh: A lot of people like Python. I have never used it, but I'm sure I would like it too if I had the time to try it. But I am chasing the dollar too. Java ain't that bad. – stepanian Mar 05 '12 at 05:33
  • 10
    He's calling it bloated because its overpopulating the global class namespace. Why have URLEncoder.encode and URLDecoder.decode when you could have URL.encode and URL.decode, or even just URLEncoder.decode? Why make it all redundant and bloaty? Because its java. – B T Sep 17 '12 at 22:34
  • 32
    And then you have to handle the UnsupportedEncodingException, even though UTF-8 should be supported pretty much everywhere. – Dave Cameron Jan 02 '13 at 05:37
  • 8
    @tc.: Java 7 introduced these constants: `StandardCharsets.US_ASCII`, `StandardCharsets.UTF_8` etc. Unfortunately, `URLEncoder.encode` does not accept a `Charset`... (but many other moethods do). – sleske May 29 '13 at 08:34
  • @sleske the constant from Java 1 is **HTTP.UTF_8** – htafoya May 30 '14 at 01:12
  • @htafoya, **wrong**. This class is found in Apache HttpClient 4.x library `org.apache.http.protocol.HTTP` class. – Buhake Sindi Jul 17 '14 at 20:59
  • 21
    Minor suggestion - use `URLEncoder.encode(, StandardCharsets.UTF_8.name())`. Using the static constant `UTF_8`'s `toString()` method as the character encoding scheme throws `java.nio.charset.IllegalCharsetNameException: java.nio.charset.CharsetICU[UTF-8]` as the `toString()` returns "java.nio.charset.CharsetICU[UTF-8]". To get the desired "UTF-8" use its `name()` method instead. – et_l May 24 '17 at 11:40
  • 2
    So instead of a simple way to encode we now have a longer more painful method that throws an exception. Why can't it just default to the lowest common denominator, it's not like I am encrypting anything. Who ever decided on this change should take an arrow to the knee. – LordWabbit May 30 '20 at 15:38
40

You should use:

URLEncoder.encode("NAME", "UTF-8");
kenorb
  • 155,785
  • 88
  • 678
  • 743
Atul Darne
  • 746
  • 7
  • 14
32

Use the class URLEncoder:

URLEncoder.encode(String s, String enc)

Where :

s - String to be translated.

enc - The name of a supported character encoding.

Standard charsets:

US-ASCII Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set ISO-8859-1 ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1

UTF-8 Eight-bit UCS Transformation Format

UTF-16BE Sixteen-bit UCS Transformation Format, big-endian byte order

UTF-16LE Sixteen-bit UCS Transformation Format, little-endian byte order

UTF-16 Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark

Example:

import java.net.URLEncoder;

String stringEncoded = URLEncoder.encode(
    "This text must be encoded! aeiou áéíóú ñ, peace!", "UTF-8");
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
1

The first parameter is the String to encode; the second is the name of the character encoding to use (e.g., UTF-8).

1

The usage of org.apache.commons.httpclient.URI is not strictly an issue; what is an issue is that you target the wrong constructor, which is depreciated.

Using just

new URI( [string] );

Will indeed flag it as depreciated. What is needed is to provide at minimum one additional argument (the first, below), and ideally two:

  1. escaped: true if URI character sequence is in escaped form. false otherwise.
  2. charset: the charset string to do escape encoding, if required

This will target a non-depreciated constructor within that class. So an ideal usage would be as such:

new URI( [string], true, StandardCharsets.UTF_8.toString() );

A bit crazy-late in the game (a hair over 11 years later - egad!), but I hope this helps someone else, especially if the method at the far end is still expecting a URI, such as org.apache.commons.httpclient.setURI().

R. Kåbis
  • 51
  • 7
0

As an additional reference for the other responses, instead of using "UTF-8" you can use:

HTTP.UTF_8

which is included since Java 4 as part of the org.apache.http.protocol library, which is included also since Android API 1.

htafoya
  • 18,261
  • 11
  • 80
  • 104
  • **Wrong**, this class is found in Apache HttpClient 4.x library `org.apache.http.protocol.HTTP` class. – Buhake Sindi Jul 17 '14 at 20:57
  • @BuhakeSindi true, I read API 1 but it was of Android not Java, either way it exists before Java 7, it is even deprecated already haha. – htafoya Jul 18 '14 at 03:33
  • no, this class **never** existed in any version of the Java JDK. Android follows the Apache HttpClient library (I won't be surprised if they took the source code from there as well). – Buhake Sindi Jul 21 '14 at 21:57
  • 3
    *warning: [deprecation] UTF_8 in HTTP has been deprecated*. – sgtdck Oct 16 '14 at 09:58