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)