0

I have no success to send NSArray to web service.
Service method is not called. But other service methods call works.
Web service method is:

[WebMethod]
        public int Method(IList items){...

My array is full of objects Items:

@interface Item : NSObject
{
    double prop1;
    double prop2;
    double prop3;
}
@property double prop1;
@property double prop2;
@property double prop3;

From objective c I try to send data like this:

NSString *soapMsg =
    [NSString stringWithFormat:
     @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
     "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
     "<soap:Body>"
     "<Method xmlns=\"http://www.mysite.com/\">"
     "<items>"
     "%@"
     "</items>"
     "</Method>"
     "</soap:Body>"
     "</soap:Envelope>", myArray
     ];

    NSURL *url = [NSURL URLWithString: @"http://...."];

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];

    [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [req addValue:@"http://www.mysite.com/Method" forHTTPHeaderField:@"SOAPAction"];
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];

    if (conn)
    {
        webData = [NSMutableData data];
    }



UPDATE

When I make array like this:

NSArray  * myArrDate = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil];

And make:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myArrDate
                                                       options:0
                                                         error:nil];

It works. But I have an array with objects User which has properties: UserId, FirstName, LastName, Email, Username, DisplayName, Country, City etc. When I try above code with array of Users application crash on that line.

1110
  • 7,829
  • 55
  • 176
  • 334

4 Answers4

6

You can send an NSArray to webservice

NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:yourArray options:NSJSONWritingPrettyPrinted error:nil];
         NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
         NSLog(@"FemaleFriendList:\n%@", jsonString);
Kanhaiya Sharma
  • 1,040
  • 8
  • 20
  • Same like prince answer. When I execute this app just crash. Without any error. – 1110 Sep 21 '13 at 07:58
  • Ok I have found that to use NSJSONSerialization I need to make new class with same properties but all to be NSString's. When I do this it work. I will accept this answer. – 1110 Sep 21 '13 at 08:53
1

You can't send an NSArray to webservice, but if you want to do so then first convert that NSArray to NSString like below

NSString *stringDelete = [YourNSArray componentsJoinedByString:@","];

then pass that NSString to webservice.Let me know whether is it working or not!!!!!!Happy Coding...!!!

NiravPatel
  • 3,260
  • 2
  • 21
  • 31
1

One option is use NSJSONSerialization to convert array into string and vice-versa which is available form iOS 5

NSArray *myArrDate = any Data ....
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myArrDate
                                                   options:0
                                                     error:nil];
if (!jsonData) {
   NSLog(@"error");
 } else {
  NSString *JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
  NSLog(@"%@",JSONString);
}

Refer convert-nsdictionary-or-nsarray-to-json-nsstring-and-vice-versa link for more info.

Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
0

You are sending an NSString (%@ , myArray) as parameter and the webservice expects an IList object. Why do not you serialize the NSArray before sending it? For example you could use JSON.

gsach
  • 5,715
  • 7
  • 27
  • 42
  • Yes. It would be great if I don't need to use some framework for this. – 1110 Nov 01 '12 at 12:51
  • I have tried now with SBJson framework but I get error: [__NSArrayM JSONRepresentation]: unrecognized selector sent to instance 0x8968a90, *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM JSONRepresentation]: unrecognized selector sent to instance 0x8968a90' – 1110 Nov 01 '12 at 13:26
  • Try using NSNumber instead of double. Can you paste your code? – gsach Nov 01 '12 at 13:40
  • For test I use simple string array: NSMutableArray* tmpArr = [[NSMutableArray alloc]init]; [tmpArr addObject:@"xx"]; [tmpArr addObject:@"yy"]; [tmpArr addObject:@"uu"]; [tmpArr addObject:@"rr"]; – 1110 Nov 01 '12 at 14:01
  • @1110 Can you log your NSArray to debugger and add that to your question so that we can see how the NSArray actually looks. Then we can think about serializing it. Is it an array of strings or other kind of objects? It should be simple. – SayeedHussain Sep 20 '13 at 12:21