AFN

作者: 小盒盒 | 来源:发表于2016-03-27 14:49 被阅读375次

AFN

http://www.jianshu.com/p/047463a7ce9b

AFN简介

AFN基本使用

  • AFHTTPSessionManager

    • 是AFN中最重要的对象之一
    • 封装了HTTP请求的常见处理
    • GET\POST请求
    • 解析服务器的响应数据
     //创建
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    
    //GET请求
    - (NSURLSessionDataTask *)GET:(NSString *)URLString
                   parameters:(id)parameters
                   success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                   failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
    //POST请求
    
  • (NSURLSessionDataTask *)POST:(NSString *)URLString
    parameters:(id)parameters
    success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
    failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure

//文件上传
- (NSURLSessionDataTask *)POST:(NSString *)URLString
                parameters:(id)parameters
 constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
                success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure

```
- 网络监控

```objc
    //网络监控
 AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
 [manager startMonitoring];

 [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"%d", status);

}];
提示:要监控网络连接状态,必须要先调用单例的startMonitoring方法
```

  • GET
// AFHTTPSessionManager内部包装了NSURLSession
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];

NSDictionary *params = @{
                         @"username" : @"xxx",
                         @"pwd" : @"xxx"
                         };

[mgr GET:@"http://120.25.226.186:32812/login" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"请求成功---%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    NSLog(@"请求失败---%@", error);
}];
  • POST
// AFHTTPSessionManager内部包装了NSURLSession
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];

NSDictionary *params = @{
                         @"username" : @"xxx",
                         @"pwd" : @"xxx"
                         };

[mgr POST:@"http://xxx/login" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"请求成功---%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    NSLog(@"请求失败---%@", error);
}];
  • 文件上传
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];

[mgr POST:@"http://xxxx/upload" parameters:@{@"username" : @"123"}
    constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    // 在这个block中设置需要上传的文件
//            NSData *data = [NSData dataWithContentsOfFile:@"/Users/wh/Desktop/placeholder.png"];
//            [formData appendPartWithFileData:data name:@"file" fileName:@"test.png" mimeType:@"image/png"];

//            [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/wh/Desktop/placeholder.png"] name:@"file" fileName:@"xxx.png" mimeType:@"image/png" error:nil];

        [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/wh/Desktop/placeholder.png"] name:@"file" error:nil];
} success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"-------%@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {

}];

相关文章

网友评论

  • 漂泊的煙烣:请问一下,AFN3.x同步请求怎么用?
  • 马铃薯蜀黍:最新的AFN底层是NSURLSession吧
    小盒盒:@马铃薯蜀黍 AFNetworking 1.0建立在NSURLConnection的基础API之上 ,AFNetworking 2.0开始使用NSURLConnection的基础API ,以及较新基于NSURLSession的API的选项。 AFNetworking 3.0现已完全基于NSURLSession的API,这降低了维护的负担,同时支持苹果增强关于NSURLSession提供的任何额外功能。由于Xcode 7中,NSURLConnection的API已经正式被苹果弃用。虽然该API将继续运行,但将没有新功能将被添加,并且苹果已经通知所有基于网络的功能,以充分使NSURLSession向前发展。
    弃用的类
    下面的类已从AFNetworking 3.0中废弃:
    AFURLConnectionOperation
    AFHTTPRequestOperation
    AFHTTPRequestOperationManager

本文标题:AFN

本文链接:https://www.haomeiwen.com/subject/eaxjlttx.html