我使用的是带有JSON响应的AFNetworking-2,它运行得很好,现在我必须将它转换成XML,而不是使用JSON,因为服务器响应是用XML表示的。在搜索完之后,我找到了这段代码,但它不起作用。
对于查尔斯,我发现请求是错误的,"Fail to parse data (org.xml.sax.SAXParseException: Content not allowed is prolog)"
请问我的问题在哪里?
我的代码:
NSString *urlString = BaseURLString;
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSString *value = @"<r_PM act=\"login\" loginname=\"1234\" password=\"12345678\" />";
NSString *message = [value stringByReplacingOccurrencesOfString:@"[\\\"" withString:@""];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod: @"POST"];
[request setValue:@"text/xml" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:[[NSString stringWithFormat:@"%@",message] dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
// Make sure to set the responseSerializer correctly
operation.responseSerializer = [AFXMLParserResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSXMLParser *XMLParser = (NSXMLParser *)responseObject;
[XMLParser setShouldProcessNamespaces:YES];
// Leave these commented for now (you first need to add the delegate methods)
XMLParser.delegate = self;
[XMLParser parse];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
[operation start];
}下面是一个运行良好的示例:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *value = @"<r_PM act=\"login\" loginname=\"1234\" password=\"12345678\"/>";
NSString *authenticationURL = @"http://demo.example.com/ex/mob/";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:authenticationURL]];
NSString *message = [value stringByReplacingOccurrencesOfString:@"[\\\"" withString:@""];
[request setHTTPMethod: @"POST"];
[request setValue:@"text/xml" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:[[NSString stringWithFormat:@"%@",message] dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[urlConnection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString *responseText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", responseText);
}发布于 2014-12-05 20:57:07
当您使用AFHTTPRequestSerializer时,您的主体是使用URL参数编码创建的。您的非AFNetworking示例使用的是XML,因此主体看起来不一样。
你会想要做这样的事情:
与其使用POST:…方便方法,不如使用序列化程序,然后手动设置并排队您的操作:
NSMutableURLRequest *request = [requestSerializer requestWithMethod:@"POST" URLString:[[NSURL URLWithString:urlString] absoluteString] parameters:parameters error:nil];
request.HTTPBody = [[NSString stringWithFormat:@"%@",message] dataUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:<# success block #> failure:<# failure block #>];
[manager.operationQueue addOperation:operation];如果您必须这样做,您可能需要子类AFHTTPRequestSerializer并为您的服务器定制一个序列化程序。
但实际上,您应该告诉您的服务器团队继续接受JSON --对于大多数应用程序来说,使用JSON要简单得多。
发布于 2014-12-08 15:53:33
要扩展Aaron的回答(您可能应该接受这个答案),如果您的服务器期待XML请求,并且正在发送XML响应,您可以执行以下操作:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPBody:[xmlRequestString dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Accept"];
NSOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSXMLParser *parser = responseObject;
parser.delegate = self;
if (![parser parse]) {
// handle parsing error here
} else {
// use parsed data here
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// handle network related errors here
}];
[manager.operationQueue addOperation:operation];在上面,我设置了两个标头,Content-Type (它通知服务器您正在发送XML请求)和Accept (它通知服务器您正在期待并将接受XML )。这些未必是必需的,但可能是良好的做法。这里有时也会使用一些变体(例如,text/xml是可能的,或者还有其他一些相关的Content-Type值),但这取决于您的服务器所期望的是什么。但目标是成为一个良好的HTTP公民,并指定这些头。
显然,这假设您也实现了NSXMLParserDelegate方法,以便实际解析implemented,但这超出了这个问题的范围。如果您不熟悉NSXMLParser,我建议您查看苹果的事件驱动的XML编程指南或谷歌的"NSXMLParser示例“或"NSXMLParser教程”以获得更多信息。
顺便说一下,我注意到您正在手动构建XML字符串。有些字段(特别是密码)可能包含在XML字段中保留的一些字符。因此,如果手动构建XML,请确保将嵌入在XML中的值中的<、>和&分别替换为<、>和&。
https://stackoverflow.com/questions/27317591
复制相似问题