我正在尝试开发这个可以与本地网络下的 C# Web 服务通信的 iOS 应用程序。我正在使用AFNetworking来实现通信。现在我已经在本教程的帮助下实现了从 Web 服务读取 xml 数据,这意味着我的应用程序可以连接到 Web 服务,我想。但我真正想做的是,将数据从应用程序传递到 Web 服务。我写了一些示例代码:
AFmyAPIClient *myClient = [AFmyAPIClient sharedClient];
NSMutableDictionary *requestArray = [NSMutableDictionary new];
NSString *details = [_listOfItems description];
float tempF= [_listOfItems totalPrice];
NSString *price = [NSString stringWithFormat:@"%f",tempF];
int tempI = 1;
NSString *table = [NSString stringWithFormat:@"%u",tempI];
[requestArray setObject:details forKey:@"details"];
[requestArray setObject:price forKey:@"price"];
[requestArray setObject:table forKey:@"table"];
[myClient postPath:@"setOrderDetails" parameters:requestArray success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success!");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];
在我构建并运行此应用程序后,它会生成以下错误消息:
2013-11-03 16:25:48.654 HLR[16149:70b] <?xml version="1.0" encoding="utf-8"?>
<int xmlns="http://localhost/service/Service1.asmx">3</int>
2013-11-03 16:25:48.844 HLR[16149:70b] Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 500" UserInfo=0x8b5b140 {NSLocalizedRecoverySuggestion=Missing parameter: orderDetails.
, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0x8b5bb70> { URL: http://10.0.0.111/service/Service1.asmx/setOrderDetails }, NSErrorFailingURLKey=http://10.0.0.111/service/Service1.asmx/setOrderDetails, NSLocalizedDescription=Expected status code in (200-299), got 500, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8b4e910> { URL: http://10.0.0.111/service/Service1.asmx/setOrderDetails } { status code: 500, headers {
"Cache-Control" = private;
"Content-Length" = 34;
"Content-Type" = "text/plain; charset=utf-8";
Date = "Sun, 03 Nov 2013 05:25:34 GMT";
Server = "Microsoft-IIS/7.5";
"X-AspNet-Version" = "2.0.50727";
"X-Powered-By" = "ASP.NET";
} }}
在我的 C# Web 服务端,我有 Web 方法,例如:
[Web Method]
public void setOrderDetails(String orderDetails)
{
//store orderDetails into a text file.
}
我是 iOS 开发的新手,我不明白调用此方法时我真正传递给 Web 服务的是什么:
[myClient postPath:@"setOrderDetails" parameters:requestArray success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success!");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];
我只是将 NSDictionary 作为参数传递给 C# Web 服务吗?如果是这样,我可以在 Web 服务端使用这个 NSDictionary 做什么?
我不知道这是否是实现将数据传递到 Web 服务的正确方法。我在这里真正想做的是将一些字符串传递给 Web 服务,我该如何实现呢?
我们欢迎所有的建议!