AFNetworking教程
2014-08-20 19:59
85 查看
转:http://www.lanrenios.com/tutorials/network/2012/1126/527.html
NSURLConnection,NSOperation,
以及其他熟悉的Foundation技术之上.它拥有良好的架构,丰富的api,以及模块化构建方式,使得使用起来非常轻松.
下面是一个得到json实例,他可以使用很轻松的方式从一个url来得到json数据:
下载AFNetworking然后尝试使用里面的例子
然后加入到自己的工程然后需要简单的设置一下子,简单AFNetworking设置
AFURLConnectionOperation:一个NSOperation实现了NSURLConnection的代理方法.
HTTPRequests:
AFHTTPRequestOperation:AFURLConnectionOperation的子类,当request使用的协议为HTTP和HTTPS时,它压缩了用于决定request是否成功的状态码和内容类型.
AFJSONRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理jasonresponse数据.
AFXMLRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理xmlresponse数据.
AFPropertyListRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理property
listresponse数据.
AFHTTPClient:捕获一个基于http协议的网络应用程序的公共交流模式.包含:
使用基本的url相关路径来只做request
为request自动添加设置httpheaders.
使用http基础证书或者OAuth来验证request
为由client制作的requests管理一个NSOperationQueue
从NSDictionary生成一个查询字符串或httpbodies.
从request中构建多部件
自动的解析httpresponse数据为相应的表现数据
在网络可达性测试用监控和响应变化.
AFImageRequestOperation:一个AFHTTPRequestOperation的子类,用于下载和处理图片.
UIImageView+AFNetworking:添加一些方法到UIImageView中,为了从一个URL中异步加载远程图片
AFNETWORKING
AFNetworking他是一个现在非常用得多的ios开发中网络开源库,它是非常的讨人喜欢的网络库,适用于iOS以及MacOSX.它构建于在(appleios开发文档)以及其他熟悉的Foundation技术之上.它拥有良好的架构,丰富的api,以及模块化构建方式,使得使用起来非常轻松.
下面是一个得到json实例,他可以使用很轻松的方式从一个url来得到json数据:
NSURL*url=[NSURLURLWithString:@"http://api.twitter.com/1/statuses/public_timeline.json"]; | |
NSURLRequest*request=[NSURLRequestrequestWithURL:url]; | |
AFJSONRequestOperation*operation=[AFJSONRequestOperationJSONRequestOperationWithRequest:requestsuccess:^(NSURLRequest*request,NSHTTPURLResponse*response,idJSON){ | |
NSLog(@"PublicTimeline:%@",JSON); | |
}failure:nil]; | |
[operationstart]; |
如何开始使用AFNETWORKING
首先需要然后加入到自己的工程然后需要简单的设置一下子,
CORE:
HTTPRequests:
listresponse数据.
HTTPCLIENT:
使用基本的url相关路径来只做request
为request自动添加设置httpheaders.
使用http基础证书或者OAuth来验证request
为由client制作的requests管理一个NSOperationQueue
从NSDictionary生成一个查询字符串或httpbodies.
从request中构建多部件
自动的解析httpresponse数据为相应的表现数据
在网络可达性测试用监控和响应变化.
IMAGES
例子程序
XMLREQUEST
NSURLRequest*request=[NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb0072175d047&cat_id=34427469792%40N01&format=rest"]]; | |
AFXMLRequestOperation*operation=[AFXMLRequestOperationXMLParserRequestOperationWithRequest:requestsuccess:^(NSURLRequest*request,NSHTTPURLResponse*response,NSXMLParser*XMLParser){ | |
XMLParser.delegate=self; | |
[XMLParserparse]; | |
}failure:nil]; | |
[operationstart]; |
IMAGEREQUEST
UIImageView*imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(0.0f,0.0f,100.0f,100.0f)]; | |
[imageViewsetImageWithURL:[NSURLURLWithString:@"http://i.imgur.com/r4uwx.jpg"]placeholderImage:[UIImageimageNamed:@"placeholder-avatar"]]; |
APICLIENTREQUEST
//AFGowallaAPIClientisasubclassofAFHTTPClient,whichdefinesthebaseURLanddefaultHTTPheadersforNSURLRequestsitcreates | |
[[AFGowallaAPIClientsharedClient]getPath:@"/spots/9223"parameters:nilsuccess:^(AFHTTPRequestOperation*operation,idresponseObject){ | |
NSLog(@"Name:%@",[responseObjectvalueForKeyPath:@"name"]); | |
NSLog(@"Address:%@",[responseObjectvalueForKeyPath:@"address.street_address"]); | |
}failure:nil]; |
FILEUPLOADWITHPROGRESSCALLBACK
NSURL*url=[NSURLURLWithString:@"http://api-base-url.com"]; | |
AFHTTPClient*httpClient=[[AFHTTPClientalloc]initWithBaseURL:url]; | |
NSData*imageData=UIImageJPEGRepresentation([UIImageimageNamed:@"avatar.jpg"],0.5); | |
NSMutableURLRequest*request=[httpClientmultipartFormRequestWithMethod:@"POST"path:@"/upload"parameters:nilconstructingBodyWithBlock:^(id<AFMultipartFormData>formData){ | |
[formDataappendPartWithFileData:imageDataname:@"avatar"fileName:@"avatar.jpg"mimeType:@"image/jpeg"]; | |
}]; | |
AFHTTPRequestOperation*operation=[[[AFHTTPRequestOperationalloc]initWithRequest:request]autorelease]; | |
[operationsetUploadProgressBlock:^(NSIntegerbytesWritten,longlongtotalBytesWritten,longlongtotalBytesExpectedToWrite){ | |
NSLog(@"Sent%lldof%lldbytes",totalBytesWritten,totalBytesExpectedToWrite); | |
}]; | |
[operationstart]; |
STREAMINGREQUEST
art]; |
相关文章推荐
- iOS开发零基础教程之AFNetWorking POST 队列请求
- AFNetworking教程
- AFNetworking速成教程
- [IOS] AFNetworking 使用教程
- AFNetworking速成教程(1)
- AFNetworking 2.0的教程
- AFNetworking速成教程
- AFNetworking速成教程
- AFNetworking速成教程
- iOS之AFNetworking 速成教程1
- Objective-c开发教程--如何在项目中使用AFNetworking
- [译]iOS-raywenderlich翻译-AFNetworking速成教程
- AFNetworking 速成教程1
- iOS之AFNetworking 速成教程2
- AFNetworking速成教程(1)
- AFNetworking 速成教程3
- AFNetworking速成教程
- AFNetworking速成教程
- iOS之AFNetworking 速成教程3
- AFNetworking速成教程(1)