首页 > 代码库 > 网络第二节——AFNworking

网络第二节——AFNworking


/**
要使用常规的AFN网络访问

  1. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    所有的网络请求,均有manager发起

  2. 需要注意的是,默认提交请求的数据是二进制的,返回格式是JSON

    1> 如果提交数据是JSON的,需要将请求格式设置为AFJSONRequestSerializer
    2> 如果返回格式不是JSON的,

  3. 请求格式

    AFHTTPRequestSerializer 二进制格式
    AFJSONRequestSerializer JSON
    AFPropertyListRequestSerializer PList(是一种特殊的XML,解析起来相对容易)

  4. 返回格式

    AFHTTPResponseSerializer 二进制格式
    AFJSONResponseSerializer JSON
    AFXMLParserResponseSerializer XML,只能返回XMLParser,还需要自己通过代理方法解析
    AFXMLDocumentResponseSerializer (Mac OS X)
    AFPropertyListResponseSerializer PList
    AFImageResponseSerializer Image
    AFCompoundResponseSerializer 组合
    */

  • (void)viewDidLoad
    {
    [super viewDidLoad];

    [self reach];
    }

pragma mark - 演练

pragma mark - 检测网络连接

  • (void)reach
    {
    /*
    AFNetworkReachabilityStatusUnknown = -1, // 未知
    AFNetworkReachabilityStatusNotReachable = 0, // 无连接
    AFNetworkReachabilityStatusReachableViaWWAN = 1, // 3G 花钱
    AFNetworkReachabilityStatusReachableViaWiFi = 2, // 局域网络,不花钱
    /
    // 如果要检测网络状态的变化,必须用检测管理器的单例的startMonitoring
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];

    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

      NSLog(@"%d", status);

    }];
    }

pragma mark - Session 下载

  • (void)sessionDownload
    {
    NSURLSessionConfiguration config = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager
    manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:config];

    NSString *urlString = @"http://localhost/itcast/videos/01.C语言-语法预览.mp4";
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL url = [NSURL URLWithString:urlString];
    NSURLRequest
    request = [NSURLRequest requestWithURL:url];

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

      // 指定下载文件保存的路径

    // NSLog(@"%@ %@", targetPath, response.suggestedFilename);

      // 将下载文件保存在缓存路径中  
      NSString *cacheDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];  
      NSString *path = [cacheDir stringByAppendingPathComponent:response.suggestedFilename];  
    
      // URLWithString返回的是网络的URL,如果使用本地URL,需要注意  
      NSURL *fileURL1 = [NSURL URLWithString:path];  
      NSURL *fileURL = [NSURL fileURLWithPath:path];  
    
      NSLog(@"== %@ |||| %@", fileURL1, fileURL);  
    
      return fileURL;

    } completionHandler:^(NSURLResponse response, NSURL filePath, NSError *error) {

      NSLog(@"%@ %@", filePath, error);

    }];

    [task resume];
    }

pragma mark - POST JSON

  • (void)postJSON
    {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    NSDictionary dict = @{@"name": @"zhangsan"};
    NSDictionary
    dict1 = @{@"name": @"wangwu"};
    NSArray *array = @[dict, dict1];
    // 设置请求格式
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    // 设置返回格式
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    [manager POST:@"http://localhost/postjson.php" parameters:array success:^(AFHTTPRequestOperation *operation, id responseObject) {

      NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];  
    
      NSLog(@"%@", result);

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

    }];

}

pragma mark - 随机文件名上传

  • (void)postUpload1
    {
    // 本地上传给服务器时,没有确定的URL,不好用MD5的方式处理
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    [manager POST:@"http://localhost/demo/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

      NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"头像1.png" withExtension:nil];  
    
      // 要上传保存在服务器中的名称  
      // 使用时间来作为文件名 2014-04-30 14:20:57.png  
      // 让不同的用户信息,保存在不同目录中  
      NSDateFormatter *formatter = [[NSDateFormatter alloc] init];  
      // 设置日期格式  
      formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";  
      NSString *fileName = [formatter stringFromDate:[NSDate date]];  
    
      [formData appendPartWithFileURL:fileURL name:@"uploadFile" fileName:fileName mimeType:@"image/png" error:NULL];

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

      NSLog(@"OK");

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

      NSLog(@"error");

    }];
    }

pragma mark - POST上传

  • (void)postUpload
    {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    // AFHTTPResponseSerializer就是正常的HTTP请求响应结果:NSData
    // 当请求的返回数据不是JSON,XML,PList,UIImage之外,使用AFHTTPResponseSerializer
    // 例如返回一个html,text...
    //
    // 实际上就是AFN没有对响应数据做任何处理的情况
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    // formData是遵守了AFMultipartFormData的对象
    [manager POST:@"http://localhost/demo/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

      // 将本地的文件上传至服务器  
      NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"头像1.png" withExtension:nil];  
    
      [formData appendPartWithFileURL:fileURL name:@"uploadFile" error:NULL];

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

      NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];  
    
      NSLog(@"完成 %@", result);

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

      NSLog(@"错误 %@", error.localizedDescription);

    }];
    }

pragma mark - JSON

  • (void)XMLData
    {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    // 返回的数据格式是XML
    manager.responseSerializer = [AFXMLParserResponseSerializer serializer];

    NSDictionary *dict = @{@"format": @"xml"};

    // 网络访问是异步的,回调是主线程的,因此程序员不用管在主线程更新UI的事情
    [manager GET:@"http://localhost/videos.php" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {

      // 如果结果是XML,同样需要使用6个代理方法解析,或者使用第三方库  
      // 第三方库第三方框架,效率低,内存泄漏  
      NSLog(@"%@", responseObject);

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

      NSLog(@"%@", error);

    }];
    }

pragma mark - JSON

  • (void)JSONData
    {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    // 原本需要拼接get访问URL ? & =
    NSDictionary *dict = @{@"format": @"json"};

    // 网络访问是异步的,回调是主线程的,因此程序员不用管在主线程更新UI的事情
    [manager GET:@"http://localhost/videos.php" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {

      NSLog(@"%@", responseObject);  
      // 提问:NSURLConnection异步方法回调,是在子线程  
      // 得到回调之后,通常更新UI,是在主线程  
      NSLog(@"%@", [NSThread currentThread]);

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

      NSLog(@"%@", error);

    }];
    }

pragma mark - POST登录

  • (void)postLogin
    {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    // 原本需要拼接get访问URL ? & =
    NSDictionary *dict = @{@"username": @"wangwu", @"password" : @"wang"};

    // 网络访问是异步的,回调是主线程的,因此程序员不用管在主线程更新UI的事情
    [manager POST:@"http://localhost/login.php" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {

      NSLog(@"%@", responseObject);  
      // 提问:NSURLConnection异步方法回调,是在子线程  
      // 得到回调之后,通常更新UI,是在主线程  
      NSLog(@"%@", [NSThread currentThread]);

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

      NSLog(@"%@", error);

    }];
    }

pragma mark - GET登录

  • (void)getLogin
    {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    // 原本需要拼接get访问URL ? & =
    NSDictionary *dict = @{@"username": @"wangwu", @"password" : @"wang"};

    // 网络访问是异步的,回调是主线程的,因此程序员不用管在主线程更新UI的事情
    [manager GET:@"http://localhost/login.php" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {

      NSLog(@"%@", responseObject);  
      // 提问:NSURLConnection异步方法回调,是在子线程  
      // 得到回调之后,通常更新UI,是在主线程  
      NSLog(@"%@", [NSThread currentThread]);

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

      NSLog(@"%@", error);

    }];
    }

@end

网络第二节——AFNworking