Use the development tool xcode4.6 and the simulator IOS 6.1. This example uses the arc mode and does not require manual autorelease.
Let's first take a look at Chinese-English Translation. I tried Japanese and translated it into English...
Without talking nonsense, go straight to the theme:
1. Go to the official website of youdao translation API (http://fanyi.youdao.com/openapi) to apply for key
Select "I Am a developer". If you apply for this service, select "I am the website owner.
Fill in the website name and address on the application page. If it is a exercise project, it can be started at will, but it must be more than 6 characters. The website address cannot be set to localhost. It is okay to write the IP address.
After entering this information, click apply. The API key and keyfrom must be remembered and will be used in the api url.
In addition, the URL of the API is also provided below. The keyfrom and API key have been replaced with the one just applied.
We recommend that you record the interface URL, keyfrom, and API key for future use.
Now you have successfully applied for an API.
2. Add the UI control to the main view of xcode
The specific methods are not one by one. I believe that xcode will be used, and they will all be supported here.
Uitextfield, uilabel, uibutton
The code for declaring variables and methods is as follows:
@ Interface mainviewcontroller:
Uiviewcontroller <uitextfielddelegate, uitabbardelegate> {
Iboutlet uitextfield * _ textfield;
Iboutlet uilabel * _ lblresult;
}
-(Ibaction) querytran :( uibutton *) sender;
Iii. querytran
Youdao return value has two data types: JSON and XML. This document uses the JSON format as an example. The data format is as follows:
Therefore, JSON data needs to be parsed. In this example, only the translation in the translation is used.
The Code is as follows:
-(Ibaction) querytran :( uibutton *) sender
{
If ([_ textfield. Text
Length] = 0)
{
Return;
}
Else
{
// The keyboard disappears.
If ([_ textfield
Isfirstresponder])
{
[_ Textfield
Resignfirstresponder];
}
Nsstring * TXT = _ textfield. text;
Nsstring * strurl = [nsstring
Stringwithformat: @ "http://fanyi.youdao.com/openapi.do? Keyfrom = xxxxxxx & Key = 1618693256 & type = Data & doctype = JSON & version = 1.1 & Q = % @ ", [txt
Stringbyaddingpercentescapesusingencoding: nsutf8stringencoding];
Nserror * err = nil;
Nsstring * strresult;
Nslog (@ "url: % @", strurl );
If (strurl! = Nil)
{
Nsurl * url = [nsurl
Urlwithstring: strurl];
Nsdata * Data = [nsdata
Datawithcontentsofurl: url];
Strresult = [self
Parsejsondatawithkey: Data];
}
If (result! = Nil)
{
Nslog (@ "Result: % @", strresult );
If (ERR)
{
Nslog (@ "error = % @", [err
Description]);
}
Else
{
_ Lblresult. Text = strresult;
}
}
}
// Parse the JSON data method and obtain the value corresponding to a key
-(Nsstring *) parsejsondatawithkey :( nsdata *) data
{
Nsstring * result = nil;
Nserror * error;
Nsdictionary * JSON = [nsjsonserialization
Jsonobjectwithdata: Data options: kniloptions
Error: & error];
If (JSON = nil)
{
Nslog (@ "JSON parse failed \ r \ n ");
Return nil;
}
Nsinteger * errcode = [[JSON
Objectforkey: @ "errorcode"] intvalue];
Nslog (@ "JSON errorcode: % d \ r \ n", errcode );
If (errcode! = 0)
{
Return nil;
}
Nsarray * array = [JSON
Objectforkey: @ "Translation"];
Result = [array objectatindex: 0];
Nslog (@ "JSON Translation: % @ \ r \ n", result );
Return result;
}
4. Run debugging to see the effect
It's okay to test the youdao API with all kinds of special characters and find that the API is quite powerful, and the blocking of errors is quite good.