2012-11-01 141 views
0

我没有成功将NSArray发送到Web服务。
服务方法未被调用。但其他服务方法调用工作。
Web服务的方法是:如何将NSArray发送到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]; 
    } 



UPDATE

当我做数组是这样的:

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等 当我尝试上面的代码与数组用户应用程序崩溃在该行。

回答

6

您可以发送NSArraywebservice

NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:yourArray options:NSJSONWritingPrettyPrinted error:nil]; 
     NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding]; 
     NSLog(@"FemaleFriendList:\n%@", jsonString); 
+0

和王子的答案一样。当我执行这个应用程序只是崩溃。没有任何错误。 – 1110

+0

我已更新问题。 – 1110

+0

好吧我发现要使用NSJSONSerialization我需要使用相同的属性创建新的类,但都是NSString的。当我这样做的时候它工作。我会接受这个答案。 – 1110

0

您正在发送一个NSString(%@,myArray)作为参数,并且webservice需要一个IList对象。为什么不在发送之前序列化NSArray?例如,你可以使用JSON。

+0

Ca您提供了一些代码示例? – 1110

+0

关于如何序列化为JSON? –

+0

是的。如果我不需要为此使用某个框架,那将会很棒。 – 1110

1

您不能发送一个NSArray到web服务,但如果你想这样做,那么首先是转换到的NSArray NSString的像下面

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

然后传递的NSString webservice.Let我知道是否它工作与否!!!!!!快乐编码... !!!

1

一种选择是使用NSJSONSerialization数组转换为字符串,反之亦然,其可用形式的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); 
} 

参考convert-nsdictionary-or-nsarray-to-json-nsstring-and-vice-versa链接查看更多信息。

+0

当我执行这个应用程序只是崩溃。没有任何错误。 – 1110

+0

我已更新问题。 – 1110