iOS 开发笔记
2017-04-07 本文已影响53人
LeeLom
目录
<h2 id="1"> 网络请求 </h2>
- <h4 id="1.1">POST方法 </h4>
需要借助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);
}];
- <h4 id="1.2">GET方法 </h4>
需要借助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