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&text="+URLEncoder.encode(string, "UTF-8")+
"&hl=en&sl="+from+"&tl="+to+"";
URL url = new URL(text);
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream(), "UTF-8"));
String json = in.readLine();
byte[] bytes = json.getBytes("UTF-8");
in.close();
return bytes;
//return text.getBytes();
}
catch (Exception e) {
return null;
}
}
}
and:
public class AbcServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain;charset=UTF-8");
resp.getWriter().println(new String(URLReader.read("pl", "en", "koń")));
}
}
When I run this i get:{"sentences"[{"trans":"end","orig":"koďż˝","translit":"","src_translit":""}],"src":"pl","server_time":30}
so utf doesnt work correctly but if i return encoded url: http://translate.google.com/translate_a/t?client=o&text=ko%C5%84&hl=en&sl=pl&tl=en
and paste at url bar i get correctly:{"sentences":[{"trans":"horse","orig":"koń","translit":"","src_translit":""}],"dict":[{"pos":"noun","terms":["horse"]}],"src":"pl","server_time":76}