// ViewController.h // FirstAFNetWorking // // Created by 张国锋 on 15/7/20. // Copyright (c) 2015年 张国锋. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end // // ViewController.m // FirstAFNetWorking // // Created by 张国锋 on 15/7/20. // Copyright (c) 2015年 张国锋. All rights reserved. // #import "ViewController.h" #import "AFNetworking.h" #import "GDataXMLNode.h" //json #define kJSONUrlString @"http://www.baidu.com:8080/free/applications/limited?currency=rmb&page=1" //xml的地址 #define kXMLUrlString @"http://wiapi.baidu.com/news/getlist4.0.php?pid=100234721&pc=20&pn=1&st=0" #define kJSONPostString @"http://网址/sjll/v1/homes/basic_data" #define kXMLPostString @"sadfasfdasdf" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self jsonGetRequest]; // [self xmlGetRequest]; // NSDictionary *dic=[NSDictionary dictionaryWithObjects:@[@"2"] forKeys:@[@"area_id"]]; // [self jsonPostRequestWith:dic]; // Do any additional setup after loading the view, typically from a nib. } -(void)jsonGetRequest{ //对json数据的Get请求 AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager]; // manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"application/json"];//告诉manager,解析的类型是json数据 manager.responseSerializer=[[AFHTTPResponseSerializer alloc]init]; [manager GET:kJSONUrlString parameters:nil success:^(AFHTTPRequestOperation * operation, id responseObject) {//responseObject 接受的数据 NSLog(@"%ld",manager.operationQueue.operationCount);//请求完之后是出对列 为 0
if ([responseObject isKindOfClass:[NSData class]]) { NSLog(@"%@",responseObject); }else if([responseObject isKindOfClass:[NSDictionary class]]){ NSLog(@"%@",responseObject); } } failure:^(AFHTTPRequestOperation * operation, NSError * error) { NSLog(@"wqrqwer%@",error); }]; // manager.operationQueue.operationCount NSLog(@"%ld",manager.operationQueue.operationCount);//manager.operationQueue.operationCount 队列里面有多少个请求 // [manager.operationQueue cancelAllOperations];//取消队列中所有的请求 } -(void)xmlGetRequest{ AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager]; // manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"text/xml"]; //报错报3840 就是服务端的数据不是严格按照xml的格式书写的。 //xml解析xcode很难解析,我们使用GData manager.responseSerializer=[[AFHTTPResponseSerializer alloc]init];//告诉AFNetWorking,我不需要你解析,我自己解析 [manager GET:kXMLUrlString parameters:nil success:^(AFHTTPRequestOperation * operation, id responseObject) { // NSLog(@"%@",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); } } failure:^(AFHTTPRequestOperation * operation, NSError * error) { NSLog(@"%@",error); }]; } -(void)jsonPostRequestWith:(NSDictionary *)dic{ AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];//这是一个单列 manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"application/json"]; [manager POST:kJSONPostString parameters:dic success:^(AFHTTPRequestOperation * operation, id responseObject) { NSLog(@"%@",responseObject); } failure:^(AFHTTPRequestOperation * operation, NSError * error) { NSLog(@"%@",error); }]; } -(void)xmlPostRequestWith:(NSDictionary *)dic{ AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager]; //manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"text/xml"];
manager.responseSerializer=[[AFHTTPResponseSerializer alloc]init]; [manager POST:kXMLPostString parameters:dic success:^(AFHTTPRequestOperation * operation, id responseObject) { NSLog(@"%@",responseObject); } failure:^(AFHTTPRequestOperation * operation, NSError * error) { NSLog(@"%@",error); }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
Cocoa Pods 管理第三方库.
时间: 2024-11-03 22:28:16