AFNetworking请求
2016-09-23 本文已影响140人
HuLL乐乐
1.get请求
+(void)getHttpRequestWithURLStr:(NSString *)urlStr withResponseBlock:(void(^)(id sender))completion{
AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//以下设置也可行
//manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"application/x-json",@"text/html", nil];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"application/x-json",@"text/html", nil]];
manager.requestSerializer.timeoutInterval = 10;
[manager.requestSerializer setValue:@"6d4dd17ce6800b2ec941627ca1137378" forHTTPHeaderField:@"apikey"];
//post如果需要传参数
//NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@"对应的值值",@"对应的key", nil];
//NSDictionary *params = @{@"key" : @"value",
// @"key" : @"value",
// @"key" : @"value"};
NSDictionary *params = nil;
//发送请求
[manager GET:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject){
NSLog(@"GET请求成功");
completion(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error:%@",error);
NSLog(@"GET请求失败");
}];
}
2.post请求参数传数组
+(void)postHttpRequestWithURLStr:(NSString *)urlStr withParametersArray:(NSArray *)parameters withResponseBlock:(void(^)(id sender))completion{
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
session.responseSerializer = [AFHTTPResponseSerializer serializer];
session.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
session.requestSerializer = [AFJSONRequestSerializer serializer];
session.responseSerializer = [AFJSONResponseSerializer serializer];
[session.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[session.requestSerializer setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[session POST:urlStr parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject)
{
NSLog(@"POST请求成功");
completion(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"POST请求失败");
}];
}
3.post请求参数传字典
+(void)postHttpRequestWithURLStr:(NSString *)urlStr withParametersDic:(NSDictionary *)parameters withResponseBlock:(void(^)(id sender))completion
{
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
session.responseSerializer = [AFHTTPResponseSerializer serializer];
session.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
session.requestSerializer = [AFJSONRequestSerializer serializer];
session.responseSerializer = [AFJSONResponseSerializer serializer];
[session.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[session.requestSerializer setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
// 参数1: url
// 参数2: body体
[session POST:urlStr parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject)
{
NSLog(@"POST请求成功");
completion(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"POST请求失败");
}];
}
总结:调用测试百度天气api
//测试
NSString *httpUrl = @"http://apis.baidu.com/heweather/pro/weather";
NSString *httpArg = @"city=beijing";
NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, httpArg];
[AFManager getHttpRequestWithURLStr:urlStr withResponseBlock:^(id sender) {
NSDictionary *dic = (NSDictionary *)sender;
if (sender)
{
NSLog(@"get==========有数据%@",dic);
}
else
{
NSLog(@"get==========有数据");
}
}];