【问题标题】:How to send NSArray to web service如何将 NSArray 发送到 Web 服务
【发布时间】:2012-11-01 11:11:48
【问题描述】:

我没有成功将 NSArray 发送到 Web 服务。
不调用服务方法。但其他服务方法调用有效。
Web服务方法是:

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

我的数组中充满了对象 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 等。 当我尝试使用 Users 数组在该行上应用程序崩溃的上述代码时。

【问题讨论】:

    标签: iphone objective-c ios asmx nsurl


    【解决方案1】:

    您可以发送NSArraywebservice

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

    【讨论】:

    • 和王子回答一样。当我执行这个应用程序只是崩溃。没有任何错误。
    • 好的,我发现要使用 NSJSONSerialization,我需要创建具有相同属性但全部都是 NSString 的新类。当我这样做时,它会起作用。我会接受这个答案。
    【解决方案2】:

    您不能将 NSArray 发送到 Web 服务,但如果您想这样做,请先将该 NSArray 转换为 NSString,如下所示

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

    然后将该 NSString 传递给 webservice。让我知道它是否正常工作!!!!!!快乐编码...!!!

    【讨论】:

      【解决方案3】:

      一种选择是使用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 链接了解更多信息。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 是的。如果我不需要为此使用一些框架,那就太好了。
        • 我现在已经尝试使用 SBJson 框架,但出现错误:[__NSArrayM JSONRepresentation]:无法识别的选择器发送到实例 0x8968a90,*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[ __NSArrayM JSONRepresentation]:无法识别的选择器发送到实例 0x8968a90'
        • 尝试使用 NSNumber 而不是 double。你能粘贴你的代码吗?
        • 为了测试我使用简单的字符串数组: NSMutableArray* tmpArr = [[NSMutableArray alloc]init]; [tmpArr addObject:@"xx"]; [tmpArr addObject:@"yy"]; [tmpArr addObject:@"uu"]; [tmpArr addObject:@"rr"];
        • @1110 您能否将您的 NSArray 记录到调试器并将其添加到您的问题中,以便我们可以看到 NSArray 的实际外观。然后我们可以考虑序列化它。它是字符串数组还是其他类型的对象?应该很简单。
        猜你喜欢
        • 1970-01-01
        • 2012-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-04
        相关资源
        最近更新 更多