How to invoke Youdao translation API (Java,http)

Source: Internet
Author: User

Apply for key

First enter Youdao translation, click "Youdao translation API" below.

Immediately after calling the data interface, follow the prompts to complete the following input boxes.

As shown, the application has been successful.

is an official example, there can be XML and JSON, JSONP and other three kinds of data formats.

Code (Java)
 Public  class testpost {     Public Static voidMain (string[] args) {NewReadbypost (). Start (); }} class readbypost extends Thread{@Override Public voidRun () {Try{URL url =NewURL ("Http://fanyi.youdao.com/openapi.do");            HttpURLConnection connection = (httpurlconnection) url.openconnection (); Connection.addrequestproperty ("Encoding","UTF-8"); Connection.setdoinput (true); Connection.setdooutput (true); Connection.setrequestmethod ("POST");            OutputStream OS = Connection.getoutputstream (); OutputStreamWriter OSW =NewOutputStreamWriter (OS); BufferedWriter BW =NewBufferedWriter (OSW); Bw.write ("Keyfrom=fadabvaa&key=522071532&type=data&doctype=json&version=1.1&q=microsoft");            Bw.flush ();            InputStream is = Connection.getinputstream (); InputStreamReader ISR =NewInputStreamReader (IS,"UTF-8"); BufferedReader br =NewBufferedReader (ISR);            String Line; StringBuilder Builder =NewStringBuilder (); while(line = Br.readline ())! =NULL) {builder.append (line);            } bw.close ();            Osw.close ();            Os.close ();            Br.close ();            Isr.close ();            Is.close ();        System.out.println (Builder.tostring ()); }Catch(Malformedurlexception e)        {E.printstacktrace (); }Catch(IOException e)        {E.printstacktrace (); }    }}

The output is as follows:

{"translation":["Microsoft"],"Basic":{"us-phonetic":"' maikr?us?ft", "phonetic":"' maikr?us?ft", "  Uk-phonetic":" ' maikr?us?ft ","explains":[" N. Microsoft Corporation (the world's leading manufacturer of PC software) "] },"Query":"Microsoft","ErrorCode":0,"Web":[{"value":["Microsoft Corporation","Microsoft","United States Microsoft"], "key":"Microsoft" },{"value":["Office software","Microsoft Office software","Microsoft Office"], "key" :"Microsoft Office"},{"value":["Microsoft Gadget"], "key":" Microsoft Gadgets "}]}

Copyright NOTICE: This article is nomasp Couvant original article, without permission is prohibited reprint! Welcome to my blog: http://blog.csdn.net/nomasp

How to invoke Youdao translation API (Java,http)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.