1

In official documentation of Google Translate Api for Java it says that we can use post-method to send more than 2K characters. https://developers.google.com/translate/v2/using_rest

However when I try to translate text more than 2k length I get error 414 (Request-URI Too Large).

StringBuilder sb = new StringBuilder();
HttpURLConnection connection = null;
try {
    URL url = new URL("https://www.googleapis.com/language/translate/v2");
    String urlParameters = "key=" + apiKey + "&source=" + shortLang1 + 
            "&target=" + shortLang2 + "&q=" + URLEncoder.encode(lyrics, "UTF-8");
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches (false);
    connection.addRequestProperty("X-HTTP-Method-Override", "GET");
    DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
    wr.writeBytes (urlParameters);
    wr.flush ();
    wr.close ();
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
    if (connection.getResponseCode() != 200) {
        return null;
    }
    String line;
    while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
    }
    reader.close();
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (connection != null) {
        connection.disconnect();
    }
}

UPDATE: I got it, the code above is right. Finally I realize that I got this error not from Google Translate service but from my proxy on Google App Engine.

user1049280
  • 5,176
  • 8
  • 35
  • 52

1 Answers1

0

What the document you linked to is saying is that you use the POST method and you put the parameters into the request body ... not the request URL.

Reference:

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thank you for reply, but I'm afraid I don't understand you - I do POST with putting parameters to the request body. Just like here: http://www.javadb.com/sending-a-post-request-with-parameters-from-a-java-class – user1049280 Aug 26 '13 at 08:40