I am trying to develop this iOS app that can communicate with a C# web service under the local network. I am using AFNetworking to achieve the communication. Now I have achieved reading xml data from the web service with the help of this tutorial, which means my app can connect to the web service, I think. But what I really want to do is, passing data from the app to the web service. I have write some sample code:
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);
}];
After I build and run this app, it generates this error message:
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";
} }}
At my C# web service side, I have Web Methods like:
[Web Method]
public void setOrderDetails(String orderDetails)
{
//store orderDetails into a text file.
}
I am new to iOS developing, I don't understand what did I really pass to the web service when I invoke this method:
[myClient postPath:@"setOrderDetails" parameters:requestArray success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success!");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];
Did i just pass a NSDictionary as a parameter to the C# web service? If that is what it is, what can I do with this NSDictionary at the web service side?
I don't know whether this is the right way to achieve passing data to the web service. What I really want to do here is to pass some strings to the web service, how can I achieve that?
Any suggestions would be much appreciated!
You are passing the XML content as URL parameters. You need to pass it in the body of the request. Here are a couple of questions addressing posting XML using AFNetworking:
Example on using AFNetworking to post XML?
AFNetworking - Making an XML POST request to REST service? How to form NSDictionary parameters?