Source: http://www.bcmeng.com/api2/
Small dream yesterday and we shared the WP8.1 PowerWord API use, today continue to share the Windows Phone 8.1 Baidu translation API use method. Same as yesterday. First we need to apply for Baidu translation API key:
Application of key of Baidu translation API:
Enter http://developer.baidu.com/need a Baidu account, registered after landing. Click on the management desk at the top right, select Developer Services Management, enter the port, select Create Project. Once you've created the project, you'll be able to get your key. Like Kingsoft, to process the returned results, we must know the format of the data returned and the format of the originating request.
Description of Baidu Translation API:
http://developer.baidu.com/wiki/index.php?title=%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3%E9%A6%96%E9%A1%B5/%E7%99% be%e5%ba%a6%e7%bf%bb%e8%af%91/%e7%99%be%e5%ba%a6%e8%af%8d%e5%85%b8api%e4%bb%8b%e7%bb%8d Inside the instructions are very detailed, small dream will not be talkative.
Make a request to the Baidu translation API and process the data:
voidTranslatefromapi () {stringclient_id ="5viabptrhtejv0hmrb0zn0ov"; stringsrc =TxtSrc.Text.Trim (); Txtdst.text="being translated"; HttpWebRequest Request= (HttpWebRequest) webrequest.create ("http://openapi.baidu.com/public/2.0/bmt/translate?client_id="+ client_id +"&q="+ src +"&from="+ from+"&to="+to ); Request. Method="GET"; Request. headers["Cookies"] ="Name=value"; Request. BeginGetResponse (responsecallback, request); } Private Async voidresponsecallback (IAsyncResult result) {Try{HttpWebRequest HttpWebRequest=(HttpWebRequest) result. asyncstate; WebResponse WebResponse=httpwebrequest.endgetresponse (Result); using(Stream stream =WebResponse.GetResponseStream ())using(StreamReader reader =NewStreamReader (Stream)) { stringContent =Reader. ReadToEnd (); Jsonobject Joresponse=jsonobject.parse (content); if(Joresponse.containskey ("error_msg") ) {Txtdst.text= joresponse["error_msg"]. GetString (); } Else { if(Joresponse.containskey ("Trans_result")) { stringResultend =NULL; foreach(Jsonvalue Iteminchjoresponse["Trans_result"]. GetArray ()) {Resultend+ = Item. GetObject (). Getnamedstring ("DST") +Environment.NewLine; } awaitDispatcher.runasync (Windows.UI.Core.CoreDispatcherPriority.Normal, () ={Txtdst.text=Resultend; }); } } } } Catch{Txtdst.text="Network access Failed! "; } }
Baidu translation API using source code download:
Click I download!
WP8.1 Small Dream Dictionary Development 2: Baidu translation API use