2019独角兽企业重金招聘Python工程师标准>>>
新建继承Object类
类.h
typedef void (^SuccessBlock)(id responseObject);
typedef void(^FailureBlock)(NSString *error);
@interface 类 : NSObject
+ (void)getWithURL:(NSString *)url Params:(NSDictionary *)params success:(SuccessBlock)success failure:(FailureBlock)failure;
+ (void)postWithURL:(NSString *)url Params:(NSDictionary *)params success:(SuccessBlock)success failure:(FailureBlock)failure;
类.m
@implementation 类
+ (void)getWithURL:(NSString *)url Params:(NSDictionary *)params success:(SuccessBlock)success failure:(FailureBlock)failure{
NSString *urlString = [NSString string];
if (params) {
NSString *paramStr = [self dealWithParam:params];
urlString = [url stringByAppendingString:paramStr];
}else{
urlString = url;
}
NSString *pathURL = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; //URL中的中文转码
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:pathURL]];
request.timeoutInterval = 3;
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response,NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (data) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
success(dict);
}else{
NSHTTPURLResponse *httpResponse = error.userInfo[@"响应地址"];
if (httpResponse.statusCode != 0) {
NSString *ResponseStr = [self showErrorInfoWithStatusCode:httpResponse.statusCode];
failure(ResponseStr);
} else {
NSString *ErrorCode = [self showErrorInfoWithStatusCode:error.code];
failure(ErrorCode);
}
}
});
}];
[task resume];
}
+ (void)postWithURL:(NSString *)url Params:(NSDictionary *)params success:(SuccessBlock)success failure:(FailureBlock)failure{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:"POST"];
NSString *body = [self stringWithParam:params];
NSData *bodyData = [body dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:bodyData];
request.timeoutInterval = 3;
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *_Nullable data,NSURLResponse *_Nullable response,NSErrror *_Nullable error){
if(data) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
success(dict);
}else {
NSHTTPURLResponse *httpResponse = error.userInfo[@"响应地址"];
if(httpResponse.statusCode != 0){
NSString *responseStr = [self showErrorWithStatusCode:httpResponse.statusCode];
failure(responseStr);
}else{
NSString *errorCode = [self showErrorWithStatusCode:error.code];
failure(errorCode);
}
}
}];
[task resume];
}
+ (NSString *)stringWithParam:(NSDictionary *)param
{
NSArray *allkeys = [param allKeys];
NSMutableString *result = [NSMutableString string];
for (NSString *key in allkeys) {
NSString *string = [NSString stringWithFormat:@"%@=%@&", key, param[key]];
[result appendString:string];
}
return result;
}
+ (NSString *)showErrorIWhthStatusCode:(NSInteger)statusCode{
NSString *messgae = nil;
switch (statusCode) {
case 1:{
message = @"请求成功";
}
break;
case 0:{
message = @"请求失败!";
}
break;
default:{
}
break;
}
return message;
}