afnetworkingafnetworking 需要引入什么类库

afnetworking  时间:2021-07-16  阅读:()

IOS开发使用Afnetworking如何在后台进行网络请求使用

如何通过URL获取json数据   第一种,利用AFJSONRequestOperation,官方网站上给的例子:   NSString *str=[NSString stringWithFormat:@"/stream/0/posts/stream/global"];   NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];   NSURLRequest *request = [NSURLRequest requestWithURL:url];   // 从URL获取json数据   AFJSONRequestOperation *operation1 = [AFJSONRequestOperation JSONRequestOperationWithRequest:ess:^(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 ess:^(AFHTTPRequestOperation *operation, id responseObject) {   NSString *html = operation.responseString;   NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];   id dict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];   NSLog(@"获取到的数据为:%@",dict);   }failure:^(AFHTTPRequestOperation *operation, NSError *error) {   NSLog(@"发生错误!%@",error);   }];   NSOperationQueue *queue = [[NSOperationQueue alloc] init];   [queue addOperation:operation];   如果发生Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x14defc80 {NSUnderlyingError=0x14deea10 "bad URL", NSLocalizedDescription=bad URL这个错误,请检查URL编码格式。

有没有进行stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding   如何通过URL获取图片   异步获取图片,通过队列实现,而且图片会有缓存,在下次请求相同的链接时,系统会自动调用缓存,而不从网上请求数据。

  UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 100.0f, 100.0f, 100.0f)]; [imageView setImageWithURL:[NSURL URLWithString:@"/r4uwx.jpg"]placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]]; [self.view addSubview:imageView];   上面的方法是官方提供的,还有一种方法,   NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/wp-content/uploads/2013/01/scene.png"]];   AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:requestimageProcessingBlock:nil ess:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {   self.backgroundImageView.image = image;   } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {   NSLog(@"Error %@",error);   }];   [operation start];   如果使用第一种URLWithString: placeholderImage:会有更多的细节处理,其实实现还是通过AFImageRequestOperation处理,可以点击URLWithString: placeholderImage:方法进去看一下就一目了然了。

所以我觉得还是用第一种好。

  如何通过URL获取plist文件   通过url获取plist文件的内容,用的很少,这个方法在官方提供的方法里面没有   NSString *weatherUrl = @"/buick/kls/Buickhousekeeper.plist";   NSURL *url = [NSURL URLWithString:[weatherUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];   NSURLRequest *request = [NSURLRequest requestWithURL:url];   [AFPropertyListRequestOperation eptableContentTypes:[NSSet setWithObject:@"text/plain"]];   AFPropertyListRequestOperation *operation = [AFPropertyListRequestOperation propertyListRequestOperationWithRequest:request ess:^(NSURLRequest *request, NSHTTPURLResponse *response, id propertyList) {   NSLog(@"%@",(NSDictionary *)propertyList);   }failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id propertyList) {   NSLog(@"%@",error);   }];   [operation start];   如果稍不留神,可能就出现Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {(   "application/x-plist"   )}, got text/plain" UserInfo=0x16e91ce0 {NSLocalizedRecoverySuggestion=   ...   ...   , AFNetworkingOperationFailingURLRequestErrorKey= { }, NSErrorFailingURLKey=, NSLocalizedDescription=Expected content type {(   "application/x-plist"   )}, got text/plain, AFNetworkingOperationFailinponseErrorKey= { URL: } { status code: 200, headers {   "ept-Ranges" = bytes;   Connection = "keep-alive";   "Content-Length" = 974;   "Content-Type" = "text/plain";   Date = "Sat, 25 Jan 2014 07:29:26 GMT";   Etag = ""1014c2-3ce-4ee63e1c80e00"";   "Last-Modified" = "Wed, 25 Dec 2013 23:04:24 GMT";   Server = "nginx/1.4.2";   } }}   可能还会出现乱码,解决办法就是[AFPropertyListRequestOperation eptableContentTypes:[NSSet setWithObject:@"text/plain"]];   如何通过URL获取XML数据   xml解析使用AFXMLRequestOperation,需要实现苹果自带的NSXMLParserDelegate委托方法,XML中有一些不需要的协议格式内容,所以就不能像json那样解析,还得实现委托。

我之前有想过能否所有的XML链接用一个类处理,而且跟服务端做了沟通,结果很不方便,效果不好。

XML大多标签不同,格式也不固定,所以就有问题,使用json就要方便的多。

  第一步;在.h文件中加入委托NSXMLParserDelegate   第二步;在.m文件方法中加入代码   NSURL *url = [NSURL URLWithString:@"http://113.106.90.22:5244/sshopinfo"];   NSURLRequest *request = [NSURLRequest requestWithURL:url];   AFXMLRequestOperation *operation =   [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request ess:^(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 *)parser{   NSLog(@"解析开始!");   }   //解析起始标记   - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{   NSLog(@"标记:%@",elementName);   }   //解析文本节点   - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{   NSLog(@"值:%@",string);   }   //解析结束标记   - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{   NSLog(@"结束标记:%@",elementName);   }   //文档结束时触发   -(void) parserDidEndDocument:(NSXMLParser *)parser{   NSLog(@"解析结束!");

afnetworking 需要引入什么类库

http网络库是集XML解析,Json解析,网络图片下载,plist解析,数据流请求操作,上传,下载,缓存等网络众多功能于一身的强大的类库。

最新版本支持session,xctool单元测试。

网络获取数据一直是手机软件的重中之重,如果处理的不好

Megalayer(48元)新增 美国CN2优化线路特价服务器和VPS方案

Megalayer 商家算是新晋的服务商,商家才开始的时候主要是以香港、美国独立服务器。后来有新增菲律宾机房,包括有VPS云服务器、独立服务器、站群服务器等产品。线路上有CN2优化带宽、全向带宽和国际带宽,这里有看到商家的特价方案有增加至9个,之前是四个的。在这篇文章中,我来整理看看。第一、香港服务器系列这里香港服务器会根据带宽的不同区别。我这里将香港机房的都整理到一个系列里。核心内存硬盘IP带宽...

妮妮云80元/月,香港站群云服务器 1核1G

妮妮云的来历妮妮云是 789 陈总 张总 三方共同投资建立的网站 本着“良心 便宜 稳定”的初衷 为小白用户避免被坑妮妮云的市场定位妮妮云主要代理市场稳定速度的云服务器产品,避免新手购买云服务器的时候众多商家不知道如何选择,妮妮云就帮你选择好了产品,无需承担购买风险,不用担心出现被跑路 被诈骗的情况。妮妮云的售后保证妮妮云退款 通过于合作商的友好协商,云服务器提供2天内全额退款,超过2天不退款 物...

HaBangNet(6.95美元/月)美国vps 5TB流量/德国vps 香港双向CN2 GIA VPS

HaBangNet支持支付宝和微信支付,只是价格偏贵,之前国内用户并不多。这次HaBangNet推出三个特价套餐,其中美国机房和德国机房价格也还可以,但是香港机房虽然是双向CN2 GIA线路,但是还是贵的惊人,需要美国和德国机房的可以参考下。HaBangNet是一家成立于2014年的香港IDC商家,中文译名:哈邦网络公司,主营中国香港、新加坡、澳大利亚、荷兰、美国、德国机房的虚拟主机、vps、专用...

afnetworking为你推荐
deepfakes什么是DeepFreeze ? 怎么使用啊?settimer如何使用SetTimer MFC 够详细settimervc++6.0 settimer函数是怎么用的啊,能给个例子在讲解一下行么settimerMFC 中,全局SetTimer函数怎么用?booth算法利用补码booth计算【xy】补,其中x=0.1110,y=-0.1101新手怎么制作表格我是初学者、电脑上怎么制作表格手机建设银行网址建设银行个人网上银行使用手机浏览器访问没有登录入口。药品标准查询《中华人民名共和国卫生部药品标准》【20080802】?pps网络电视PPS网络电视是那个国家的公司开发出来的水表读数水表的读数是什么意思?怎么读?
域名服务 域名出售 已经备案域名 greengeeks vultr美国与日本 10t等于多少g 国内加速器 网站在线扫描 个人免费主页 万网空间管理 电信主机托管 聚惠网 sonya cdn加速技术 电脑显示屏不亮但是主机已开机 电脑主机配置 次时代主机 小米电视主机 sockscap教程 qq空间技术网 更多