Beyond平君

导航

iOS开发-使用第三方库AFNetWorking解析JSON和XML数据

利用第三方库AFNetWorking解析网络请求的JSON和MXL数据具有很多方便的地方。

第三方库的下载地址:https://github.com/AFNetworking/AFNetworking

导入的包和宏定义

 1 #import "ViewController.h"
 2 #import "AFNetworking.h"
 3 #import "GDataXMLNode.h"
 4 
 5 //json地址
 6 #define kJSONUrlString @"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1"
 7 
 8 //xml的地址
 9 #define kXMLUrlString @"http://wiapi.hexun.com/news/getlist4.0.php?pid=100234721&pc=20&pn=1&st=0"
10 
11 //json Post提交方式
12 #define kJsonPostUrlString @"http://api2.dfc.cn/sjll/v1/homes/basic_data"
13 
14 //xml Post提交方式
15 #define kXMLPostUrlString @""

1.对JSON Get方式的请求方式

 1 //对JSON Get方式的请求方式
 2 - (void)jsonGetRequest
 3 {
 4     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
 5     manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
 6     [manager GET:kJSONUrlString parameters:nil success:^(AFHTTPRequestOperation *operation,id responseObject){
 7         if ([responseObject isKindOfClass:[NSData class]]) {
 8             NSLog(@"responseObjectData = %@",responseObject);
 9         }
10         else if ([responseObject isKindOfClass:[NSDictionary class]])
11         {
12             NSLog(@"responseObjectDict = %@",responseObject);
13         }
14     } failure:^(AFHTTPRequestOperation *operation,NSError *error){
15         
16     }];
17     //获取队列中有多少个请求
18     NSInteger count = manager.operationQueue.operationCount;
19     NSLog(@"count = %ld",count);
20     //取消队列中所有的请求
21     [manager.operationQueue cancelAllOperations];
22 }

2.对XML 的Get方式的请求方式

这里还使用了由Google提供的一个第三方库GData对Xml文件的解析,github下载地址:https://github.com/graetzer/GDataXML-HTML

//错误:3840  ---就是表示服务器段的数据不是严格的按照xml的格式书写的
//xml解析
- (void)xmlGetRequest
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/xml"];
    //自己生成一个响应的序列化对象,不使用系统给的方式
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
    [manager GET:kXMLUrlString parameters:nil success:^(AFHTTPRequestOperation *operation,id responseObject){
        
        GDataXMLDocument *doc=[[GDataXMLDocument alloc] initWithData:responseObject encoding:NSUTF8StringEncoding error:nil];
        NSString *path = @"/doc/focus/frame/title";
        NSArray *titles=[doc nodesForXPath:path error:nil];
        for (GDataXMLElement *title in titles) {
            NSLog(@"%@",title.stringValue);
        }
        //NSLog(@"requestObject = %@",responseObject);
    
    } failure:^(AFHTTPRequestOperation *operation,NSError *error){
    
        NSLog(@"error = %@",error);
        
    }];
}

3.对JSON 的POST的请求方式

//post请求数据操作JSON数据
- (void)jsonPostRequest:(NSDictionary *)dict
{
    //单例对象
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    [manager POST:kJsonPostUrlString parameters:dict success:^(AFHTTPRequestOperation *operation,id responseObject){
        NSLog(@"responseObject = %@",responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation,NSError *error){
    
        NSLog(@"error = %@",error);
        
    }];
}

4.对XML的Post方式请求方式

 1 //post方式请求xml数据
 2 - (void)xmlPostRequest:(NSDictionary *)dict
 3 {
 4     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
 5     //manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/xml"];
 6     //使用自己创建的对象取解析xml
 7     manager.responseSerializer = [AFHTTPResponseSerializer alloc];
 8     [manager POST:kXMLPostUrlString parameters:dict success:^(AFHTTPRequestOperation *operation,id responseObject){
 9         
10         NSLog(@"responseObject = %@",responseObject);
11     
12     } failure:^(AFHTTPRequestOperation *operation,NSError *error){
13         
14         NSLog(@"error = %@",error);
15     
16     }];
17 }

 

  

 

posted on 2015-07-21 20:25  Beyond平君  阅读(2355)  评论(0编辑  收藏  举报