最近開始接觸ios項目的開發。之前使用本地數據庫做的是offline的project。現在開始嘗試去從其他平台上拿數據。ios project是基於mac系統上的。但需要去調用windows系統上的服務。微軟有一個不錯的框架wcf.
話不多說,直接看代碼吧!
首先是wcf服務。
數據交換實體
/// <summary>
/// Customer實體類
/// </summary>
[DataContract]
public class
{
[DataMember(Order = 0)]
public string Name { get; set; }
[DataMember(Order = 1)]
public string DayOfResiger { get; set; }
}
服務契約
[ServiceContract]
public interface Iservice1
{
/// <summary>
/// 创建客戶
/// </summary>
[OperationContract]
[WebGet(UriTemplate = "Customer/{name}",ResponseFormate = webMessageFormat.Json)]
Customer GetCustomer(string name);
[OperationContract]
[WebGet(UriTemplate = "CustomerXML/{name}",ResponseFormat = WebMessageFormat.Xml)]
Customer GetCustomerXml(string name);
[OperateionContrat]
[WebInvoke {UriTemplate = "Customer/ {name/{dayofresiger}",Method = "POST",ResponseFormat = WebMessageFormat.Json}]
Customer CreateCustomer(string name,string dayofresiger);
}
服務實現
- public class Service1 : IService1
- {
- public Customer GetCustomer(string name)
- {
- return new Customer { Name = name, DayOfResiger = new DateTime(2006, 10, 23).ToString() };
- }
- public Customer GetCustomerXML(string name)
- {
- return new Customer { Name = name, DayOfResiger = new DateTime(2006, 10, 23).ToString() };
- }
- public Customer CreateCustomer(string name, string dayOfResiger)
- {
- return new Customer { Name = name, DayOfResiger = dayOfResiger };
- }
- }
在web.config中将绑定方式改成webHttpBinding
服務器上發佈服務(iis)
接下來ios端
使用ASIHTTPRequest,是直接在CFNetwork上做的开源项目,提供了一个很方便更强大的HTTP网络传输的封装。
定义下面三个方法對應三个button的点击事件:
- - (IBAction)fetXML:(id)sender;
- - (IBAction)fetJson:(id)sender;
- - (IBAction)createJson:(id)sender;
具體實現
- - (IBAction)fetXML:(id)sender
- {
- NSURL *url = [NSURL URLWithString:@"http://192.168.112.220:2194/Service1.svc/CustomerXML/Koson"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request startSynchronous];
- NSError *error = [request error];
- if (!error) {
- NSString *response = [request responseString];
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"xml格式"
- message:response
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alertView show];
- [alertView release];
- }
- }
- - (IBAction)fetJson:(id)sender
- {
- NSURL *url = [NSURL URLWithString:@"http://192.168.112.220:2194/Service1.svc/Customer/Koson"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request startSynchronous];
- NSError *error = [request error];
- if (!error) {
- NSString *response = [request responseString];
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"json格式"
- message:response
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alertView show];
- [alertView release];
- }
- }
- - (IBAction)createJson:(id)sender
- {
- NSURL *url = [NSURL URLWithString:@"http://192.168.112.220:2194/Service1.svc/Customer/Koson/2006-09-20"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request setRequestMethod:@"POST"];
- [request startSynchronous];
- NSError *error = [request error];
- if (!error) {
- NSString *response = [request responseString];
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"创建k客戶"
- message:response
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alertView show];
- [alertView release];
- }
- }