How to send NSArray to web service

一曲冷凌霜 提交于 2019-12-12 09:26:18

问题


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.


回答1:


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);



回答2:


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...!!!




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/13175811/how-to-send-nsarray-to-web-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!