如何使用 AFNetworking 3.x 在 ios、objective-C 中使用来自 SOAP webservice 的数据
Posted
技术标签:
【中文标题】如何使用 AFNetworking 3.x 在 ios、objective-C 中使用来自 SOAP webservice 的数据【英文标题】:How to use AFNetworking 3.x to consume data from SOAP webservice in ios, objective-C 【发布时间】:2015-12-28 05:08:51 【问题描述】:我正在开发一个使用来自 Web 服务的数据的应用程序。我尝试使用 afnetworking 3.x 来使用来自 Web 服务的数据。我在某个地方卡住了。不知道从哪里开始。希望您的帮助。
NSString *mainapiKey = [NSString stringWithFormat:@"12345"];
NSString *urlStringgetairport = [NSString stringWithFormat:@"http://mobileapi.uk/"];
NSURL *getairportUrl = [NSURL URLWithString:urlStringgetairport];
NSMutableURLRequest *finalRequest = [NSMutableURLRequest requestWithURL:getairportUrl];
NSString *soapmessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<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/\">\n"
"<soap:Body>\n"
"<GetAirport xmlns=\"http://mobileapi.uk/\">\n"
"<Authkey>%@</Authkey>\n"
"<AirportCode>%@</AirportCode>\n"
"</GetAirport>\n"
"</soap:Body>\n"
"</soap:Envelope>\n"
,mainapiKey, @""];
NSData *soapdata = [soapmessage dataUsingEncoding:NSUTF8StringEncoding];
NSString *messageLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapmessage length]];
[finalRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[finalRequest addValue:@"http://mobileapi.uk/GetAirport" forHTTPHeaderField:@"SOAPAction"];
[finalRequest addValue:@"mobileapi.uk" forHTTPHeaderField:@"HOST"];
[finalRequest addValue:messageLength forHTTPHeaderField:@"Content-Length"];
[finalRequest setHTTPMethod:@"POST"];
[finalRequest setHTTPBody:soapdata];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:urlStringgetairport parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
NSLog(@"output :%@", responseObject);
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
];
webservice 输出是一个 json
以正常方式成功获取数据
【问题讨论】:
如果可能的话,向你展示 Json 输出。 【参考方案1】:更改以下代码
[manager GET:urlStringgetairport parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
NSLog(@"output :%@", responseObject);
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
];
到此代码
manager.responseSerializer = [AFJSONResponseSerializer serializer];//only if your response type is JSON
NSURLSessionDataTask *task = [manager dataTaskWithRequest:finalRequest completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error)
NSLog(@"output :%@", responseObject);
];
[task resume];
您的代码中的问题是,您创建了 url 请求并且没有使用 AFNetworking API,您为 URL 调用了 GET 方法。仅使用您正在加载的代码 - 意味着将 url 放入浏览器中的相同效果。
【讨论】:
这给出了这个警告/62:32: Incompatible pointer types assigning to 'AFHTTPResponseSerializer<AFURLResponseSerialization> * _Nonnull' from 'AFJSONRequestSerializer * _Nonnull'
我没有收到任何警告,运行代码后它是否正常工作?
对不起,这是我的问题。但这里的输出为空。
也打印以下内容并让我知道输出 NSLog(@"Raw response MIMEType %@",response.MIMEType); NSLog(@"原始响应 textEncodingName %@",response.textEncodingName);
我检查了这些,都是空的【参考方案2】:
是的,输出是 Json。将该 ID responseObject
分配给 NSDictionary
,如下所示:
NSDictionary *dict = responseObject;
现在,您可以使用此字典对象提取如下值:
NSString *yourString = [dict valueForKey:@"your-key"];
NSMutableArray *arr = [[NSMutableArray alloc] init];
arr = [dict ObjectForKey:@"your-key"];
【讨论】:
不,我的意思是它不通过成功区域,它通过区域。在前面的afnetworking中,有afhttprequest操作。通过使用这个,可以调用请求。在这里,这不会调用请求。为什么?我对GET:
使用什么喊叫
对不起,我误会了,check this【参考方案3】:
要获取数据,您可以使用以下代码
NSString *mainapiKey = [NSString stringWithFormat:@"12345"];
NSString *urlStringgetairport = [NSString stringWithFormat:@"http://mobileapi.uk/"];
NSURL *getairportUrl = [NSURL URLWithString:urlStringgetairport];
NSMutableURLRequest *finalRequest = [NSMutableURLRequest requestWithURL:getairportUrl];
NSString *soapmessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<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/\">\n"
"<soap:Body>\n"
"<GetAirport xmlns=\"http://mobileapi.uk/\">\n"
"<Authkey>%@</Authkey>\n"
"<AirportCode>%@</AirportCode>\n"
"</GetAirport>\n"
"</soap:Body>\n"
"</soap:Envelope>\n"
,mainapiKey, @""];
NSData *soapdata = [soapmessage dataUsingEncoding:NSUTF8StringEncoding];
NSString *messageLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapmessage length]];
[finalRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[finalRequest addValue:@"http://mobileapi.uk/GetAirport" forHTTPHeaderField:@"SOAPAction"];
[finalRequest addValue:@"mobileapi.uk" forHTTPHeaderField:@"HOST"];
[finalRequest addValue:messageLength forHTTPHeaderField:@"Content-Length"];
[finalRequest setHTTPMethod:@"POST"];
[finalRequest setHTTPBody:soapdata];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//From here replace your code
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error)
NSData *data = responseObject;
NSDictionary *dictTemp = [NSDictionary dictionaryWithXMLData:data]; //XMLDictionary will use to parse xml
NSLog(@"%@",dictTemp);
];
[dataTask resume];
如果您的响应是 json 格式,则将 manager.responseSerializer = [AFHTTPResponseSerializer serializer];
替换为代码 manager.responseSerializer = [AFJSONResponseSerializer serializer];
【讨论】:
以上是关于如何使用 AFNetworking 3.x 在 ios、objective-C 中使用来自 SOAP webservice 的数据的主要内容,如果未能解决你的问题,请参考以下文章