ios 网络请求对afnetworking 的封装

 我来答
gl...h@163.com
2017-10-28 · 超过31用户采纳过TA的回答
知道答主
回答量:82
采纳率:33%
帮助的人:39.6万
展开全部
  何通URL获取json数据
  第种利用AFJSONRequestOperation官网站给例:
  NSString *str=[NSString stringWithFormat:@""];

  NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

  NSURLRequest *request = [NSURLRequest requestWithURL:url];

  // URL获取json数据

  AFJSONRequestOperation *operation1 = [AFJSONRequestOperation JSONRequestOperationWithRequest:requestsuccess:^(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:@""];

  NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

  NSURLRequest *request = [NSURLRequest requestWithURL:url];

  AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

  [operation setCompletionBlockWithSuccess:^(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:@""]placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]]; [self.view addSubview:imageView];
  面官提供种

  NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@""]];

  AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:requestimageProcessingBlock:nil success:^(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 = @"";

  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];

  稍留神能现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 {

  "Accept-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 addAcceptableContentTypes:[NSSet setWithObject:@"text/plain"]];

  何通URL获取XML数据

  xml解析使用AFXMLRequestOperation需要实现苹自带NSXMLParserDelegate委托XML些需要协议格式内容所能像json解析实现委托我前想能否所XML链接用类处理且跟服务端做沟通结便效XML标签同格式固定所问题使用json要便

  第步;.h文件加入委托NSXMLParserDelegate

  第二步;.m文件加入代码

  NSURL *url = [NSURL URLWithString:@""];

  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 *)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(@"解析结束");
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式