iOS 开发笔记

2017-04-07  本文已影响53人  LeeLom

目录


<h2 id="1"> 网络请求 </h2>

需要借助AFNetworking

    NSString *url = @"http://xxxxxxxxxxxxxxxxxxxx";
    NSDictionary *param = @{@"key":@"value"};
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [manager POST:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"成功%@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败%@", error);
    }];

需要借助AFNetworking

    NSString *url = @"http://xxxxxxxxxxxxxxxxxxxx; //网址
    NSDictionary *param = @{@"key":@"value"};//参数
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSLog(@"KEY :%@",[json valueForKey:@"KEY"]);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"错误:%@",error);
    }];
image.png
上一篇下一篇

猜你喜欢

热点阅读