-2

我想将通过访问 Web 服务获得的 NSData 转换为 NSArray。我该怎么做?

这是我想从 NSData 转换的结构:

NSArray:
  NSDictionary1 : roomName departmentName
  NSDictionary2 : roomName departmentName
  NSDictionary3 : roomName departmentName
  NSDictionary4 : roomName departmentName
  NSDictionary5 : roomName departmentName
  ...

Web 服务的 GetRoomList 方法返回一个列表。列表的结构如上。代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *soapMessage = [NSString stringWithFormat:@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><m:GetRoomList xmlns:m=\"http://http://portNumber/ilacDirektif.svc/\"><departId xsi:type=\"xsd:int\">0</departId><roomId xsi:type=\"xsd:int\">0</roomId></m:GetRoomList></SOAP-ENV:Body></SOAP-ENV:Envelope>"];

    NSURL *url=[NSURL URLWithString:@"http://port number/ilacDirektif.svc/"];

    NSMutableURLRequest *theRequest= [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];

    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"http://tempuri.org/ilacDirektif.svc" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest addValue:@"port number" forHTTPHeaderField:@"Host"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLResponse* response = nil;
    NSData *data =[NSURLConnection sendSynchronousRequest: theRequest returningResponse: &response error: nil];
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:data];
    if(array){NSLog(@"%@",array); }else{ NSLog(@"Failed"); }
    if (data) {NSLog(@"%@",data);} else { NSLog(@"Failed");}
}

错误:

2013-05-31 09:59:45.208 IlacOrder[18513:11303]-[__NSCFData getFileSystemRepresentation:maxLength:]:无法识别的选择器发送到实例 0x7560860 2013-05-31 09:59:45.222 IlacOrder[18513:11303] * 终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因:“-[__NSCFData getFileSystemRepresentation:maxLength:]:无法识别的选择器发送到实例 0x7560860”*First throw call stack: (0x1c8e012 0x10cbe7e 0x1d194bd 0x1c7dbbc 0x1c7d94e 0xacc7b4 0xacc762 0xafac85 0xb22c7a 0x2a55 0xf4817 0xf4882 0xf4b2a 0x10bef5 0x10bfdb 0x10c286 0x10c381 0x10ceab 0x10d4a3 0x10d098 0x2460 0x10df705 0x16920 0x168b8 0xd7671 0xd7bcf 0xd6d38 0x4633f 0x46552 0x243aa 0x15cf8 0x1be9df9 0x1be9ad0 0x1c03bf5 0x1c03962 0x1c34bb6 0x1c33f44 0x1c33e1b 0x1be87e3 0x1be8668 0x1365c 0x1ded 0x1d15 0x1 ) libc++abi.dylib:终止调用抛出异常

.svc 中的函数:

public List<DepartmentRoom>GetRoomList(int roomId,int departId()){
  return manager.GetRoomList(roomId,departId);
}

获取房间列表:

   public List<DepartmentRoom> GetRoomList(int departId,int roomId){
    var cmd = OracleHelper.GetOracleCommand(_conn, StoredProcedure.Procedure1);

                if (_conn.State == ConnectionState.Closed)
                    _conn.Open();

                OracleCommandBuilder.DeriveParameters(cmd);

                cmd.Parameters["P_DEPARTID"].Value = departId;
                cmd.Parameters["P_ROOMID"].Value = roomId;

                OracleDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                object o = cmd.Parameters["P_REF"].Value;

                var roomList = new List<DepartmentRoom>();
                while (dr.Read())
                {
                    var departmentRoom = new DepartmentRoom
                    {
                        DepartId = Convert.ToInt32(dr["DEPARTID"]),
                        DepartmentName = dr["DEPART"].ToString(),
                        RoomId = Convert.ToInt32(dr["ROOMID"]),
                        RoomName = dr["ROOM"].ToString()
                    };
                    roomList.Add(departmentRoom);
                }
                return roomList;
            }
4

2 回答 2

2

也许值得阅读您尝试使用的方法和类的文档。unarchiveObjectWithFile:将文件路径作为参数,而不是NSData. 在文档中查找以 anNSData作为输入的方法留给您作为练习。

于 2013-05-31T07:42:16.367 回答
0

这对我有用:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:REQIP]];

NSData *myPostData = [[NSString stringWithFormat:@"{\"departId\":%d,\"roomId\":%d}",departId,roomId] dataUsingEncoding:NSUTF8StringEncoding];

NSMutableData *myMutablePostData = [NSMutableData dataWithData:myPostData];

[request setPostBody:myMutablePostData];
[request setRequestMethod:@"GET"];
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request setDelegate:self];
[request startSynchronous];


NSMutableArray *list =[[NSMutableArray alloc]init];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:[[request responseString]dataUsingEncoding:NSUTF8StringEncoding]options:NSJSONReadingMutableContainers error:nil ];

for(NSDictionary *theItem in [jsonObjects objectForKey:@"RequestMethodResult"]){
    [list addObject:[[ClassModel alloc]initWithDictionary:theItem]];
}

return list;

ClassModel 是 NSMutableArray 的每个对象项所在的 Dictionary 的结构。

于 2014-01-27T08:40:17.973 回答