//个人觉得 sdk开发时候 最好不要用第三方 最好可以用最原始的方法 替换 此处仅做sdk封装使用 留存
+(void)GET:(NSString *)urlStr params:(NSDictionary *)params sucess:(void (^)(NSURLSessionDataTask *task, id responseObject))sucess failure:(void (^)(NSURLSessionDataTask *task, NSError * error))failure
{
urlStr = [self URLEncodedStringValue:params url:urlStr];
NSURL *URL = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"GET"];
NSURLSession *session = [NSURLSession sharedSession];
__block NSURLSessionDataTask *task= nil;
task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
failure(task,error);
} else {
NSError *jsonError = nil;
id notesJSON =
[NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&jsonError];
if (!jsonError) {
if (sucess) {
sucess(task,notesJSON);
}
} else {
if (failure) {
failure(task,jsonError);
}
}
}
}];
[task resume];
}
+ (NSString *)URLEncodedStringValue:(NSDictionary *)Parameter url:(NSString *)urlStr
{
if (Parameter) {
urlStr = [urlStr stringByAppendingString:@"?"];
NSArray *keys =[Parameter allKeys];
for (int i=0;i<keys.count;i++) {
NSString *key = [keys objectAtIndex:i];
NSString *value = [Parameter objectForKey:key];
if (i==0) {
urlStr = [urlStr stringByAppendingFormat:@"%@=%@",key,value];
} else {
urlStr = [urlStr stringByAppendingFormat:@"&%@=%@",key,value];
}
}
}
urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];
return urlStr;
}