title: Java - how to encode URL path for non Latin characters
body: <p>Currently there is <code>final URL url = new URL(urlString);</code> but I run into server not supporting non-ASCII in path.</p>
  
  <p>Using Java (Android) I need to encode URL from</p>
  
  <pre><code>http://acmeserver.com/download/agc/fcms/儿子去哪儿/儿子去哪儿.png
  </code></pre>
  
  <p>to</p>
  
  <pre><code>http://acmeserver.com/download/agc/fcms/%E5%84%BF%E5%AD%90%E5%8E%BB%E5%93%AA%E5%84%BF/%E5%84%BF%E5%AD%90%E5%8E%BB%E5%93%AA%E5%84%BF.png
  </code></pre>
  
  <p>just like browsers do.</p>
  
  <p>I checked <code>URLEncoder.encode(s, "UTF-8");</code> but it also encodes <code>/</code> slashes</p>
  
  <pre><code>http%3A%2F%2acmeserver.com%2Fdownload%2Fagc%2Ffcms%2F%E5%84%BF%E5%AD%90%E5%8E%BB%E5%93%AA%E5%84%BF%2F%E5%84%BF%E5%AD%90%E5%8E%BB%E5%93%AA%E5%84%BF.png
  </code></pre>
  
  <p><strong>Is there way to do it simply without parsing string that the method gets?</strong></p>
  
  <p>from <a href="http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars" rel="nofollow">http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars</a></p>
  
  <blockquote>
    <p>B.2.1 Non-ASCII characters in URI attribute values Although URIs do
    not contain non-ASCII values (see [URI], section 2.1) authors
    sometimes specify them in attribute values expecting URIs (i.e.,
    defined with %URI; in the DTD). For instance, the following href value
    is illegal:</p>
    
    <p><code>&lt;A href="http://foo.org/Håkon"&gt;...&lt;/A&gt;</code></p>
    
    <p>We recommend that user agents adopt the following convention for
    handling non-ASCII characters in such cases:</p>
    
    <ol>
    <li>Represent each character in UTF-8 (see [RFC2279]) as one or more
    bytes.  </li>
    <li>Escape these bytes with the URI escaping mechanism (i.e., by
    converting each byte to %HH, where HH is the hexadecimal notation of
    the byte value).</li>
    </ol>
  </blockquote>