1

I'm trying to develop a Java application with Gson to use an external api to get a simple text translation. Below is code and output. I've used my API key at the designated location for API key in the HTTP post URL Could you please help me get this working? Thank you very much.

// CODE

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("URL");

StringEntity input = new StringEntity(json);
input.setContentType("application/json");
post.setEntity(input);
HttpResponse response = client.execute(post);
System.out.println(response.getStatusLine());  

//OUTPUT

HTTP/1.1 415 Unsupported Media Type
root
  • 157
  • 1
  • 3
  • 16

1 Answers1

1

As you can see, you received 404 Not Found response from the server. Often it means that you send a request to wrong URL.

You should use https version of request as shown in documentation. So the correct URL looks like this: https://translate.yandex.net/api/v1.5/tr.json/translate?lang=en-ru&key=API-KEY.

Edit

Regarding the second issue, try to add header for your post.

post.addHeader("Content-Type", "application/json");

Edit 2

I looked into documentation, and they don't use body in request. Just add text to URL like this:

https://translate.yandex.net/api/v1.5/tr.json/translate?text=live&lang=en-ru&key=API-KEY`

Don't forget to remove setting entity, I mean delete post.setEntity(input) and related stuff.

Nominalista
  • 4,632
  • 11
  • 43
  • 102
  • Thanks for reply.This is the URI I used with the API key – root Jun 16 '18 at 12:58
  • You mean you put there the correct API key, but this URL is just to show us an example? – Nominalista Jun 16 '18 at 13:01
  • Thank you. It worked!. But I receive a different error code now. :( – root Jun 16 '18 at 13:10
  • You should edit your question or create a new one, related that problem. Also show the `textConv`, it's hard to guess how it's represented. – Nominalista Jun 16 '18 at 13:12
  • Done!. Thanks for your help. Really appreciate it. :) – root Jun 16 '18 at 13:12
  • textConv class is at the bottom – root Jun 16 '18 at 13:13
  • Tried adding the post.addheader(..). But same output. I think it's something wrong with my json conversion. Can't figure out what exactly :( – root Jun 16 '18 at 13:27
  • I tried adding the "text=live" part to URL, but same code 415.. If I'm to delete "input entity" how can I do the gson conversion? Could you please explain a bit more. I'm a noob actually and trying understand this. Thanks so much – root Jun 16 '18 at 14:15
  • Oh I understand u're saying remove the input parameter and set the http post like this? post.addHeader("Content-Type", "application/json"); post.setEntity(new StringEntity(json)); – root Jun 16 '18 at 14:25
  • I get this response {"text":"Text to be converted Hello World"} [Content-Type: text/plain; charset=ISO-8859-1,Content-Length: 43,Chunked: false] HTTP/1.1 415 Unsupported Media Type – root Jun 16 '18 at 14:26
  • Your text to translate is in the URL, `text=live` is an example of translating the word "live". So remove `post.setEntity(new StringEntity(json));` and related stuff, because you don't need the body in request. – Nominalista Jun 16 '18 at 19:30
  • oh right. Understood.One more thing. Could you please explain how I display the response? I have this code to print the response "System.out.println(response.getStatusLine()); System.out.println(response.getEntity());" and the respose printed is, "HTTP/1.1 200 OK org.apache.http.conn.BasicManagedEntity@117159c0 " Thank you – root Jun 17 '18 at 15:05
  • You've already asked for two things in one question. If you have another issue, ask a new question or search the Internet, because it's a common situation to handle JSON response (e.g. [here](https://stackoverflow.com/questions/2845599/how-do-i-parse-json-from-a-java-httpresponse)). Furthermore if my solution resolved your problem, please accept the answer. – Nominalista Jun 17 '18 at 15:11