iOS——网络请求(原生)
2017-07-27 00:00
218 查看
新建继承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;
}
类.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;
}
相关文章推荐
- iOS原生网络请求使用总结
- iOS——网络请求(原生)
- IOS 原生网络请求
- iOS 原生网络请求封装
- iOS 原生网络请求
- iOS 原生网络请求
- iOS 【原生API NSURLSession 网络请求(重新弄一份 格式不对!!!!!!!!!!)】
- iOS-原生网络请求
- iOS 【原生API NSURLConnection 网络请求】
- iOS 原生网络请求写法
- iOS网络请求第三方框架:MKNetworkKit介绍及简单使用
- ASIHTTPRequest 详解, ios http网络请求
- IOS开发--网络篇-->网络请求(HTTP协议)小结
- iOS网络编程(二) 自定义请求网络类----推荐用于需要请求过程片段数据
- ios网络请求时网址带汉字时转码
- IOS中http网络请求get和post的区别
- iOS 网络数据请求带中文参数转换
- iOS开发--用户点击频繁,多个异步网络请求取消问题?
- iOS音频的后台播放总结(后台网络请求歌曲,Remote控制,锁屏封面,各种打断)
- iOS开发--Swift 基于AFNetworking 3.0的网络请求封装