iOS 网络请求之ASIHttpRequest 封装

因所在公司项目久远,网络请求一直用的是ASIHTTPRequest,一步一步写起来太麻烦,故对其封装下,废话不多说直接上代码:

ASIHTTPRequest GitHub链接

demo地址

1.引入ASI,本人使用CocoaPods三方库管理工具

platform :ios, '7.0'

target 'ASINetworking' do

pod 'ASIHTTPRequest'

......

end

2. 先创建一个工具类

ASIHttpUtil.h

/**

*  请求完成回调方法

*/

typedef void (^CompleteBlock)(id json, NSString *stringData);

typedef void (^FailedBlock)(NSError *error);

typedef void (^ProgressBlock)(float progress);

/**

*  GET请求,有参数

*

*  @param path 接口路径,不能为空

*  @param paramsDic 请求的参数的字典,参数可为nil, 例如:NSDictionary *params = @{@"key":@"value"}

*  @param completeBlock 请求完成块,返回 id JSON, NSString *stringData;

*  @param failed 请求失败块,返回 NSError *error;

*/

+ (ASIHTTPRequest *)getRequestWithPath:(NSString *)path params:(NSDictionary *)paramsDic completed:(CompleteBlock)completeBlock failed:(FailedBlock)failed;

/**

*  POST请求,有参数;

*

*  @param path 接口路径,不能为空;

*  @param paramsDic 请求的参数的字典,参数可为nil, 例如:NSDictionary *params = @{@"key":@"value"}

*  @param completeBlock 请求完成块,返回 id JSON, NSString *stringData;

*  @param failed 请求失败块,返回 NSError *error;

*

*  @return 返回ASIHTTPRequest的指针,可用于 NSOperationQueue操作

*/

+ (ASIHTTPRequest *)postRequestWithPath:(NSString *)path params:(NSDictionary *)paramsDic completed:(CompleteBlock)completeBlock failed:(FailedBlock)failed;

ASIHttpUtil.m

#pragma mark GET请求

+ (ASIHTTPRequest *)getRequestWithPath:(NSString *)path params:(NSDictionary *)paramsDic completed:(CompleteBlock)completeBlock failed:(FailedBlock)failed{

NSString *urlString = [NSString stringWithFormat:@"%@",path];

urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:urlString];

__weak ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

request.requestMethod = @"GET";

for (NSString *key in sg_httpHeaders.allKeys) {

if (sg_httpHeaders[key] != nil) {

[request addRequestHeader:key value:sg_httpHeaders[key]];

}

}

[request setCompletionBlock:^{

NSError *errorForJSON = [NSError errorWithDomain:@"请求数据解析为json格式,发出错误" code:2014 userInfo:@{@"请求数据json解析错误": @"中文",@"serial the data to json error":@"English"}];

id jsonData = [NSJSONSerialization JSONObjectWithData:[request responseData] options:0 error:&errorForJSON];

completeBlock(jsonData,request.responseString);

}];

[request setFailedBlock:^{

failed([request error]);

}];

[request setNumberOfTimesToRetryOnTimeout:requestTimesTry];

[request setTimeOutSeconds:requestTimeLimited];

[request startAsynchronous];

return request;

}

#pragma mark POST请求

+ (ASIHTTPRequest *)postRequestWithPath:(NSString *)path params:(NSDictionary *)paramsDic completed:(CompleteBlock)completeBlock failed:(FailedBlock)failed{

NSString *urlString = [NSString stringWithFormat:@"%@",path];

urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:urlString];

__weak ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

request.requestMethod = @"POST";

for (NSString *key in sg_httpHeaders.allKeys) {

if (sg_httpHeaders[key] != nil) {

[request addRequestHeader:key value:sg_httpHeaders[key]];

}

}

[paramsDic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

[request setPostValue:obj forKey:key];

}];

[request setCompletionBlock:^{

NSError *errorForJSON = [NSError errorWithDomain:@"请求数据解析为json格式,发出错误" code:2014 userInfo:@{@"请求数据json解析错误": @"中文",@"serial the data to json error":@"English"}];

id jsonData = [NSJSONSerialization JSONObjectWithData:[request responseData] options:0 error:&errorForJSON];

completeBlock(jsonData, request.responseString);

}];

[request setFailedBlock:^{

failed([request error]);

}];

[request setNumberOfTimesToRetryOnTimeout:requestTimesTry];

[request setTimeOutSeconds:requestTimeLimited];

[request startAsynchronous];

return request;

}

本文只针对于GET请求方法和POST请求方法做了介绍,Demo中还详细介绍了文件上传及下载等方法的封装,有需要的朋友可以下载查看,希望对你有所帮助。

你可能感兴趣的:(iOS 网络请求之ASIHttpRequest 封装)