【发布时间】:2014-12-05 14:02:03
【问题描述】:
我正在使用带有 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);
}
【问题讨论】:
-
您正在使用 Charles:您是否比较了这两个请求的样子?显然它们是不同的,您拥有查看请求的理想工具。
-
顺便说一句,你对
[urlConnection start]的调用应该被删除。 -
我已删除
[urlConnection start]... 在 NSURLConnection 中,请求看起来像这样但在 AFNetworking @987654327 @此消息在 Charles 请求 XML 中。 @Rob -
在请求中?还是回应?您可以仔细检查一下,因为在响应中看到这一点我不会感到惊讶,但在请求中看到这一点没有意义。
-
@Rob 我仔细检查了它是否在请求中。回复是
<?xml-stylesheet type="text/xsl" href="/pr/xsl/genel/error.xsl"?> <root> <res t="-2"> <a o="MS" d="Wrong req" /> </res> </root>
标签: ios afnetworking afnetworking-2