我没有成功将 NSArray 发送到 Web 服务。
不调用服务方法。但其他服务方法调用有效。
Web服务方法是:
[WebMethod]
public int Method(IList items){...
我的数组充满了对象项目:
@interface Item : NSObject
{
double prop1;
double prop2;
double prop3;
}
@property double prop1;
@property double prop2;
@property double prop3;
从目标 c 我尝试发送这样的数据:
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];
}
更新
当我像这样制作数组时:
NSArray * myArrDate = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil];
和做:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myArrDate
options:0
error:nil];
有用。但是我有一个包含User
具有属性的对象的数组:UserId, FirstName, LastName, Email, Username, DisplayName, Country, City
等。当我尝试上面的代码时,该行的用户应用程序崩溃。