Problem requesting AFNetworking XML request

I use AFNetworking-2JSON with the response, and it works fine, and now I need to convert it to XML instead of using JSON, because the server response is in XML. After I searched, I got to this code, but it does not work.

With Charles, I found that the request is incorrect "Fail to parse data (org.xml.sax.SAXParseException: Content not allowed is prolog)"

Please, where is my problem?

My code is:

    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 that works 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);
}
+4
source share
2 answers

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

- :

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 - .

+3

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, XML-, . NSXMLParser, Apple XML Google " NSXMLParser" " NSXMLParser" .


, , XML . ( ) , XML. , XML , , <, > & , XML &lt;, &gt; &amp; .

+3

All Articles