WP8.1 Small Dream Dictionary Development 2: Baidu translation API use

Source: Internet
Author: User

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

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.