AFNetworking XML请求问题 [英] AFNetworking XML Request Issue

查看:163
本文介绍了AFNetworking XML请求问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 AFNetworking-2 与JSON响应一起使用,并且工作正常,现在我必须将其转换为XML而不是使用JSON,因为服务器响应位于XML。搜索后,我找到了此代码,但无法正常工作。

I'm using AFNetworking-2 with JSON response and it was working fine and now I have to convert it to XML instead of using JSON because the server response is in XML. After I've searched I reached with this code but it is not working.

与Charles一起,我发现请求是错误的未能解析数据(org.xml.sax.SAXParseException:不允许的内容是序言)

With Charles I found the request is wrong "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];
}

这里有一个很好的例子:

Here is an example which is working fine:

- (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);
}


推荐答案

使用 AFHTTPRequestSerializer 时,您的主体是使用URL表单参数编码创建的。您的非AFNetworking示例是使用XML,因此主体看起来不一样。

When you use AFHTTPRequestSerializer your body is created using URL Form Parameter encoding. Your non-AFNetworking example is using XML, so the body looks different.

您需要做s像这样的事情:

You'll want to do something like this:

而不是使用 POST:…便捷方法,而是使用序列化程序,然后进行设置手动使操作入队:

Instead of using the POST:… convenience method, use the serializer and then set up and enqueue your operation manually:

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 并为您的服务器创建自定义序列化器。

If you have to do this a bunch, you might want to subclass AFHTTPRequestSerializer and make a custom serializer for your server.

但是实际上您应该告诉您的服务器团队继续接受JSON-这工作起来要简单得多适用于大多数应用。

But really you should just tell your server team to keep accepting JSON - it's much simpler to work with for most applications.

这篇关于AFNetworking XML请求问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆