网络数据请求-创新互联
网络请求----请求微博信息:需要导入AFNetworking文件和UIKit+AFNetworking文件
创新互联主要从事成都网站建设、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务桐梓,十余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220 #import
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
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
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前标题:网络数据请求-创新互联
链接分享:http://hbruida.cn/article/dhhgsi.html