Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. private static String translate123(String lang, String input) throws IOException {
  2. String urlStr = "https://dictionary.yandex.net/api/v1/dicservice.json/lookup?key=dict.1.1.20170324T082045Z.5089076809ee28a9.ffa2a8f71e0c7c4a629cfa04aa38975a721fdaa6&";
  3. URL urlObj = new URL(urlStr);
  4. HttpsURLConnection connection = (HttpsURLConnection)urlObj.openConnection();
  5. connection.setRequestMethod("POST");
  6. connection.setDoOutput(true);
  7. DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
  8. dataOutputStream.writeBytes("lang="+ lang + "&text=" + URLEncoder.encode(input, "UTF-8"));
  9. InputStream response = connection.getInputStream();
  10. String json = null;
  11. Scanner scn = new Scanner(response);
  12. while(scn.hasNextLine()){
  13. json = scn.nextLine();
  14. }
  15. int start = json.indexOf("[");
  16. int end = json.indexOf("]");
  17. String translated = json;
  18. i++;
  19. //вот это вот заморочка для русского. Иначе эту шляпу делать не надо
  20. //хотя она не мешает и с ангельским
  21. byte ptext[] = translated.getBytes();
  22. String encodedStr = new String(ptext, "UTF-8");
  23. return encodedStr;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement