iphone - 如何将 NSArray 发送到 Web 服务

标签 iphone objective-c ios asmx nsurl

我没有成功将 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;

我尝试从 objective-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 数组的上述代码时,应用程序在该行崩溃。

最佳答案

你可以发送一个 NSArraywebservice

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

关于iphone - 如何将 NSArray 发送到 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13175811/

相关文章:

iphone - 以编程方式切换到另一个选项卡并切换到第一个 View Controller ?

ios - 如何在 Settings.bundle 中的 xCode 中添加额外的 plists 以便与 InAppSettingsKit iPhone 库一起使用?

objective-c - NSMetadataQuery 的 NSPredicate 是否匹配包含目标日期的 kMDItemAccessedDates (数组)?

ios - All Exceptions 断点未找到 "unrecognized selector sent to instance"错误

ios - 从公共(public)中心一起旋转一组 UIView

ios - 使用 ios 和 Storyboard编程 UIViewControllers 周期

iphone - 无法使用 Socialize SDK 注销 Facebook

html - Safari 视口(viewport)错误,固定位置和底部或顶部的问题

ios - 前缀头导入取消了库对相同方法的实现

objective-c - 使用 [NSBundle mainBundle] 写入文件失败