AFNetWorking的简单使用.doc_第1页
AFNetWorking的简单使用.doc_第2页
AFNetWorking的简单使用.doc_第3页
AFNetWorking的简单使用.doc_第4页
AFNetWorking的简单使用.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

AFNetWorking的简单使用首先,对于AFNetWorking,我们要知道它的框架结构,AFURLConnectionOperation继承于NSObject,AFHTTPRequestOperation继承于AFURLConnectionOperation,然后JSON,XML,plist,Image数据继承于AFHTTPRequestOperation。一,AFNetworking的使用1. 将AFNetWorking文件夹导入项目2. 添加类库 Security.framework、MobileCoreServices.framework、 SystemConfiguration.framework3. 在使用的地方 #import “AFNetworking.h1. 根据基本的URL构造除完整的一个URL,然后通过这个完整的URL获得一个NSURL对象,然后根据这个url获得一个NSURLRequest。 2. AFJSONRequestOperation是一个完整的类,整合了从网络中获取数据并对JSON进行解析。 3. 当请求成功,则运行成功块。在本例中,把解析出来的天气数据从JSON变量转换为一个字典(dictionary),并将其存储在字典中。 4. 如果运行出问题了,则运行失败块(failure block),比如网络不可用。如果failure block被调用了,将会通过提示框显示错误信息。例:如何通过URL获取json数据第一种,利用AFJSONRequestOperationNSString *str=NSStringstringWithFormat:/stream/0/posts/stream/global;NSURL *url = NSURL URLWithString:str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding;NSURLRequest *request = NSURLRequest requestWithURL:url;/ 从URL获取json数据AFJSONRequestOperation *operation1 = AFJSONRequestOperation JSONRequestOperationWithRequest:request success:(NSURLRequest*request, NSHTTPURLResponse *response, NSDictionary* JSON) NSLog(获取到的数据为:%,JSON); failure:(NSURLRequest *request,NSHTTPURLResponse *response, NSError *error, id data) NSLog(发生错误!%,error);operation1 start;第二种方法,利用AFHTTPRequestOperation 先获取到字符串形式的数据,然后转换成json格式,将NSString格式的数据转换成json数据,利用IOS5(基本不会使用)自带的json解析方法:NSString *str=NSString stringWithFormat:/stream/0/posts/stream/global;NSURL *url = NSURL URLWithString:str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding;NSURLRequest *request = NSURLRequest requestWithURL:url;AFHTTPRequestOperation *operation = AFHTTPRequestOperation alloc initWithRequest:request;operation setCompletionBlockWithSuccess:(AFHTTPRequestOperation*operation, idresponseObject) NSString *html = operation.responseString;NSData* data=html dataUsingEncoding:NSUTF8StringEncoding;iddict=NSJSONSerialization JSONObjectWithData:data options:0 error:nil;NSLog(获取到的数据为:%,dict);failure:(AFHTTPRequestOperation*operation, NSError *error) NSLog(发生错误!%,error);NSOperationQueue *queue = NSOperationQueue alloc init;queue addOperation:operation;2,AFNetWorking异步加载图片(1)#import “UIImageView+AFNetworking.h” (2)UIImageView *imageView = UIImageView alloc initWithFrame:CGRectMake(40, 80, 40, 40); _weak UIImageView *_imageView = imageView; imageView setImageWithURLRequest:NSURLRequest alloc initWithURL:NSURL URLWithString:/images/wsymbols01_png_64/wsymbol_0001_sunny.png placeholderImage:UIImage imageNamed:placeholder.png success:(NSURLRequest *request,NSHTTPURLResponse *response, UIImage *image) _imageView.image = image; _imageView setNeedsDisplay; failure:(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) ; ; self.view addSubview:imageView;如何通过URL获取图片NSURLRequest *request = NSURLRequestrequestWithURL:NSURLURLWithString:/wp-content/uploads/2013/01/scene.png;AFImageRequestOperation *operation = AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil success:(NSURLRequest*request, NSHTTPURLResponse *response, UIImage *image) self.backgroundImageView.image = image; failure:(NSURLRequest*request, NSHTTPURLResponse *response, NSError *error) NSLog(Error %,error);operation start;如何通过URL获取plist文件NSString *weatherUrl = /buick/kls/Buickhousekeeper.plist;NSURL *url = NSURL URLWithString:weatherUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding;NSURLRequest *request = NSURLRequest requestWithURL:url;AFPropertyListRequestOperation addAcceptableContentTypes:NSSet setWithObject:text/plain;AFPropertyListRequestOperation *operation = AFPropertyListRequestOperation propertyListRequestOperationWithRequest:request success:(NSURLRequest*request, NSHTTPURLResponse *response, id propertyList) NSLog(%,(NSDictionary *)propertyList);failure:(NSURLRequest *request,NSHTTPURLResponse *response, NSError *error, id propertyList) NSLog(%,error);operation start;如何通过URL获取XML数据xml解析使用AFXMLRequestOperation,需要实现苹果自带的NSXMLParserDelegate委托方法,XML中有一些不需要的协议格式内容,所以就不能像json那样解析,还得实现委托。我之前有想过能否所有的XML链接用一个类处理,而且跟服务端做了沟通,结果很不方便,效果不好。XML大多标签不同,格式也不固定,所以就有问题,使用json就要方便的多。第一步;在.h文件中加入委托NSXMLParserDelegate第二步;在.m文件方法中加入代码NSURL *url = NSURLURLWithString:2:5244/sshopinfo;NSURLRequest *request = NSURLRequest requestWithURL:url;AFXMLRequestOperation *operation =AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:(NSURLRequest*request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) XMLParser.delegate = self;XMLParser setShouldProcessNamespaces:YES;XMLParser parse;failure:(NSURLRequest *request,NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) NSLog(%,error);operation start;第三步;在.m文件中实现委托方法/在文档开始的时候触发-(void)parserDidStartDocument:(NSXMLParser *)parserNSLog(解析开始!);/解析起始标记- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDictNSLog(标记:%,elementName);/解析文本节点- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)stringNSLog(值:%,string);/解析结束标记- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qNameNSLog(结束标记:%,elementName);/文档结束时触发-(void) parserDidEndDocument:(NSXMLParser *)parserNSLog(解析结束!);如何使用AFHTTPClient进行web service操作AFHTTPClient处理GET和 POST请求.做网页的朋友们这个方法用的比较多。在要经常调用某个请求时,可以封装,节省资源。BaseURLString = /downloads/weather_sample/;NSURL *baseURL = NSURL URLWithString:NSString stringWithFormat:BaseURLString;NSDictionary *parameters = NSDictionary dictionaryWithObject:json forKey:format;AFHTTPClient *client = AFHTTPClient alloc initWithBaseURL:baseURL;client registerHTTPOperationClass:AFJSONRequestOperation class;client setDefaultHeader:Accept value:text/html;client postPath:weather.phpparameters:parameters success:(AFHTTPRequestOperation *operation,id responseObject) NSString* newStr = NSString alloc initWithData:responseObject encoding:NSUTF8StringEncoding;NSLog(POST请求:%,newStr);failure:(AFHTTPRequestOperation*operation, NSError *error) NSLog(%,error);client getPath:weather.phpparameters:parameters success:(AFHTTPRequestOperation *operation,id responseObject) NSString* newStr = NSString alloc initWithData:responseObject encoding:NSUTF8StringEncoding;NSLog(GET请求:%,newStr);failure:(AFHTTPRequestOperation*operation, NSError *erro

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论