Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>byte[] bytes = json.getBytes("UTF-8"); </code></pre> <p>gives you a UTF-8 bytes sequences so URLReader.read also give you UTF-8 bytes sequences</p> <p>but you tried to decode with without specifying the encoder, i.e. <code>new String(URLReader.read("pl", "en", "koń"))</code> so Java will use your system default encoding to decode (which is not UTF-8)</p> <p>Try :</p> <pre><code>new String(URLReader.read("pl", "en", "koń"), "UTF-8") </code></pre> <p><strong>Update</strong></p> <p>Here is fully working code on my machine:</p> <pre><code>public class URLReader { public static byte[] read(String from, String to, String string) { try { String text = "http://translate.google.com/translate_a/t?" + "client=o&amp;text=" + URLEncoder.encode(string, "UTF-8") + "&amp;hl=en&amp;sl=" + from + "&amp;tl=" + to + ""; URL url = new URL(text); URLConnection conn = url.openConnection(); // Look like faking the request coming from Web browser solve 403 error conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)"); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String json = in.readLine(); byte[] bytes = json.getBytes("UTF-8"); in.close(); return bytes; //return text.getBytes(); } catch (Exception e) { System.out.println(e); // becarful with returning null. subsequence call will return NullPointException. return null; } } } </code></pre> <p>Don't forget to escape ń to \u0144. Java compiler may not compile Unicode text properly so it is good idea to write it in plain ASCII.</p> <pre><code>public class AbcServlet extends HttpServlet { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("text/plain;charset=UTF-8"); byte[] read = URLReader.read("pl", "en", "ko\u0144"); resp.getOutputStream().write(read) ; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload