Afnetworking request XML



HTTP protocol is the most common way for server to communicate data with client. Generally speaking, the data format is JSON and XML. JSON format can be parsed. How to parse XML?
——This article is based on afnetworking version 3.2.1

reference material:……
FocusAfnetworking request XML

Generally speaking, we instantiate an HTTP requestAFHTTPSessionManagerObject and set some properties:

manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", @"text/plain",nil];
manager.requestSerializer.timeoutInterval = 30.0f;

We have no settings heremanagerofresponseSerializerProperties,responseSerializerThe default type isAFJSONResponseSerializerObviously, to request data in XML format, you can’t useAFJSONResponseSerializer
Here are the key codes:

AFXMLParserResponseSerializer *response = [AFXMLParserResponseSerializer serializer];
manager.responseSerializer = response;

Some online statements are

AFJSONResponseSerializer *response = [AFJSONResponseSerializer serializer];
response.acceptableContentTypes = [NSSet setWithObjects:@"text/xml", nil];
manager.responseSerializer = response;

This usage is wrong! hereresponseSerializerMust be of typeAFXMLParserResponseSerializer, as for why such a solution appears, it may be because the respondent simply guessed that the error return was “unable to receive data of text / XML type” and did not verify it; In other words, this method can be used in lower versions of afnetworking.

The usage is as follows

[manager GET:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    Nslog (@ "lenovoid login request result:% @", responseobject);
    NSXMLParser *xmlparser = responseObject;
    [xmlparser setDelegate:self];
    [xmlparser parse];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    Nslog (@ "lenovoid login request failed:% @", [error localizeddescription]);

To achieveNSXMLParserDelegateagreement

#pragma mark - NSXMLParser delegate
- (void)parserDidStartDocument:(NSXMLParser *)parser{
    Nslog (@ "XML document start");

-(void)parserDidEndDocument:(NSXMLParser *)parser{
    Nslog (@ "end of XML document");

- (void)parser:(NSXMLParser *)parser foundElementDeclarationWithName:(NSString *)elementName model:(NSString *)model{

The usage of NSXMLParser is indicated in resources:
Afnetworking request XML

Afnetworking is the most basic IOS development skill. The demo of specific implementation is not given here. The most important thing about XML parsing is settingAFHTTPSessionManagerObjectresponseSerializerThe attribute value isAFXMLParserResponseSerializerObject.