网络请求----请求微博信息:需要导入AFNetworking文件和UIKit+AFNetworking文件
#import <Foundation/Foundation.h>
typedefvoid(^FinishDidBlock)(AFHTTPRequestOperation *operation,id result);
typedefvoid(^FailureBlock)(AFHTTPRequestOperation *operation,NSError *error);
@interface DataService : NSObject
+ (AFHTTPRequestOperation *)requestWithURL:(NSString *)url
params:(NSMutableDictionary *)paramsDic
httpmethod:(NSString *)httpMethod
finishDidBlock:(FinishDidBlock)finishDidBlock
failureBlock:(FailureBlock)failureBlock;
@end
#import "DataService.h"
//#import "AFNetworking.h"
#define BASE_URL @"https://open.weibo.cn/2/"
@implementationDataService
+ (AFHTTPRequestOperation *)requestWithURL:(NSString *)url
params:(NSMutableDictionary *)paramsDic
httpmethod:(NSString *)httpMethod
finishDidBlock:(FinishDidBlock)finishDidBlock
failureBlock:(FailureBlock)failureBlock
{
if (paramsDic == nil) {
paramsDic =[NSMutableDictionarydictionary];
}
//拼接URL
NSMutableString *urlStr = [NSMutableStringstringWithFormat:@"%@%@",BASE_URL,url];
//参数处理
NSUserDefaults *defaults = [NSUserDefaultsstandardUserDefaults];
NSDictionary *sinaWeiboInfo = [defaults objectForKey:@"SinaWeiboAuthData"];
NSString *accessTokenKey =[sinaWeiboInfo objectForKey:@"AccessTokenKey"];
if (accessTokenKey.length == 0) {
returnnil;
}
[paramsDic setObject:accessTokenKey forKey:@"access_token"];
//创建请求管理对象
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
//设置请求数据的解析方式
manager.requestSerializer = [AFHTTPRequestSerializerserializer];
AFHTTPRequestOperation *operation = nil;
if ([httpMethod isEqualToString:@"GET"]) {
operation = [manager GET:urlStr parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
//请求成功
NSLog(@"AF-GET请求成功");
//安全判断
if (finishDidBlock) {
finishDidBlock(operation,responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//请求失败
NSLog(@"AF-GET请求失败");
//安全判断
if (failureBlock) {
failureBlock(operation,error);
}
}];
}
elseif ([httpMethod isEqualToString:@"POST"]){
//POST请求
BOOL isFile = NO;
for (NSString *key in paramsDic) {
id value = [paramsDic objectForKey:key];
if ([value isKindOfClass:[NSDataclass]]) {
//有文件类型的参数
isFile = YES;
break;
}
}
if (!isFile) {
//没有文件
operation = [manager POST:urlStr parameters:paramsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
//请求成功
NSLog(@"AF-POST(非文件)请求成功");
//安全判断
if (finishDidBlock) {
finishDidBlock(operation,responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//请求失败
NSLog(@"AF-POST(非文件)请求失败");
//安全判断
if (failureBlock) {
failureBlock(operation,error);
}
}];
} else {
//有文件
operation = [manager POST:urlStr parameters:paramsDic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
for (NSString *key in paramsDic) {
id value = [paramsDic objectForKey:key];
if ([value isKindOfClass:[NSDataclass]]) {
//往formData表单中添加data数据
[formData appendPartWithFileData:value
name:key fileName:key mimeType:@"p_w_picpath/jpeg"];
}
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
//请求成功
NSLog(@"AF-POST(带文件)请求成功");
//安全判断
if (finishDidBlock) {
finishDidBlock(operation,responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//请求失败
NSLog(@"AF-POST(带文件)请求失败");
//安全判断
if (failureBlock) {
failureBlock(operation,error);
}
}];
}
}
//设置返回数据的解析方式
operation.responseSerializer = [AFJSONResponseSerializerserializerWithReadingOptions:NSJSONReadingMutableContainers];
return operation;
}
@end
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。