【发布时间】:2015-12-17 08:30:15
【问题描述】:
我正在使用 SOAP 网络服务。如果我在浏览器中检查网络服务,则响应为
[{"sno":null,"AirportCode":"YQM","Airport":"Moncton Airport","City":"Moncton","Country":"Canada"}]
但是当我签入 iOS 时,响应带有 xml tags.like this
[{"sno":null,"AirportCode":"YQM","Airport":"Moncton Airport","City":"Moncton","Country":"Canada"}]<?xml version="1.0" encoding="utf-8"?>
这是我的方法....
NSString *autenticationkey = [NSString stringWithFormat:@"authkey"];
NSString *acode = @"YQM";
//NSString *empty = @"";
NSString *soapMessage = [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>"
"<GetAirport xmlns=\"http://mapi.uk/\">"
"<Authkey>%@</Authkey>"
"<AirportCode>%@</AirportCode>"
"</GetAirport>"
"</soap:Body>"
"</soap:Envelope>"
,autenticationkey,acode];
NSData *soapData = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
NSString *mainurlName = [NSString stringWithFormat:@"http://somuturlop/GetAirport"];
NSURL *getcodeserviceUrl = [NSURL URLWithString:mainurlName];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:getcodeserviceUrl];
NSString *messageLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"http://mopi.uk" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:@"mpi.uk" forHTTPHeaderField:@"HOST"];
[theRequest addValue:messageLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:soapData];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:theRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"data Task With Request error:%@", error);
return;
}
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode != 200) {
NSLog(@"data Task With Request HTTP status code :%ld", (long)statusCode);
return;
}
}
webData = [[NSMutableData alloc] init];
[webData appendData:data];
NSLog(@"Received bytes from the server :%lu", (unsigned long)[webData length]);
resultString = [[NSMutableString alloc] initWithBytes:[webData bytes] length:[webData length] encoding:NSUTF8StringEncoding];
resultString 给出了这个结果。我想把这个 json 数据放入一个数组中。
我试过这个。但它没有工作
resultStringOne = [[NSMutableString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
NSError *jsonError;
NSData *jsonData = [resultStringOne dataUsingEncoding:NSUTF8StringEncoding];
NSArray *getArray = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&jsonError]
【问题讨论】:
-
为什么你认为浏览器没有得到相同的标签?
-
Checkout link 用于目标中的 xml 解析。浏览器解析器和目标解析器处理的xml也不同,输出也不同,因为在目标c中,你可以额外获得xml标签,这样你就可以在ios端很好地处理它。
-
你查看浏览器响应的源代码了吗? xml 标签可能在浏览器视图中不可见。
-
我想用这个响应数据(想把这个数据放到一个数组中)怎么做
-
你说结果不同,但没有提供证据。正如@thst 所写,浏览器中的源视图显示什么?
标签: ios objective-c json soap