zoukankan      html  css  js  c++  java
  • iOS

    #5、AFNetworking 2.6使用方法

    >2.6版本 支持 iOS7以上,而且支持NSURLConnectionOperation

    >3.0版本 支持 iOS7以上 NSURLConnectionOperation被废弃了
    ```

    //获取网络请求管理器

    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    ```

    ###1) 执行get请求
    ```

    //GET 请求地址
    //parameters 请求参数
    //success 请求成功的回调方法
    //failure 请求失败的回调方法

    //responseObject 返回请求成功获得的数据
    //AFN 可以帮我们自动解析json

    ```

    ###2)执行get请求2
    ```

    // parameters 一般是字典

    NSDictionary * params = @{@"username":@"haha",@"password":@“123"};

    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];

    [manager GET:@"http://192.168.1.200/login.php" parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {

    NSLog(@"%@",responseObject);

    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {

    NSLog(@"%@",error);

    }];

    *将GET 改为POST 则为执行POST请求*
    ```

    ###3)解析xml
    ```

    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];

    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //改成AFXMLParserResponseSerializer 格式
    manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
    //设置可以响应的类型
    // manager.responseSerializer.acceptableContentTypes

    [manager GET:@"http://192.168.1.2/train.xml" parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {

    NSLog(@"%@",responseObject);

    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
    NSLog(@"%@",error);
    }];

    ```
    ###4)上传data

    ```
    POST 上传地址
    parameters 文本参数
    constructingBodyWithBlock 上传文件的block,有可能多次调用
    success 上传成功
    failure 上传失败
    [manager POST:@"http://192.168.1.200/post/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {

    formData 上传的数据
    FileData 上传文件的data
    name 上传文件的key
    FileURL 本地路径
    fileName 服务器上的名字
    mimeType 上传资源的类型

    NSURL * url = [[NSBundle mainBundle] URLForResource:@"111.png" withExtension:nil];

    //上传文件

    [formData appendPartWithFileURL:url name:@"userfile00" fileName:@"2016030211459.png" mimeType:@"image/png" error:NULL];

    //上传data
    [formData appendPartWithFileData:data name:@"userfile00" fileName:@"123" mimeType:@"image/png"];

    } success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
    NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
    NSLog(@"%@",error);
    }];

    ```

    #6、AFNetworking 3.0使用方法
    ###1、AFNetworking3.0 和AFNetworking2.6的部分区别

    >3.0版本删除了所有的 NSURLConnection 方法

    >并将所有的AFHTTPRequestOperationManager 改为 AFHTTPSessionManager

    >AFHTTPSessionManager * manager1 = [AFHTTPSessionManager manager];

    >AFURLSessionManager *manager2 = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    >AFURLSessionManager ≈ NSURLSession
    设置manager2的同时需要设置NSURLSessionConfiguration

    ###2、具体方法

    #####1)监测网络
    ```

    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

    NSLog(@"Reachability: %@“,AFStringFromNetworkReachabilityStatus(status));

    }];

    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    ```

    #####2)上传文件
    ```

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://192.168.1.200/post/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
    {
    [formData appendPartWithFileURL:[[NSBundle mainBundle] URLForResource:@"111.png" withExtension:nil] name:@"userfile00" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
    } error:nil];

    NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {

    if (error) {
    NSLog(@"Error: %@", error);
    } else {
    NSLog(@"%@ %@", response, responseObject);
    }

    }];
    [uploadTask resume];
    ```

    #####3)下载文件
    ```

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSURL *URL = [NSURL URLWithString:[@"http://192.168.1.2/demo.json" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

    //下载地址
    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];

    NSLog(@"%@",[documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]]);

    return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];

    }
    completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error)
    {
    NSLog(@"File downloaded to: %@", filePath);
    }];
    //filePath == [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]

    [downloadTask resume];

    ```

  • 相关阅读:
    vscode snippet
    OpenGL Type
    [转] fio参数详解
    [转] openchannel SSD( OCSSD)
    [转]linux内存管理
    proc/meminfo && hugepage
    [转] 从free到 page cache
    [转]linux网络协议栈(1)——链路层
    [转]linux 网络协议栈(1)——网络设备
    [转]linux网络协议栈(1)——socket buffer
  • 原文地址:https://www.cnblogs.com/PSSSCode/p/5508543.html
Copyright © 2011-2022 走看看