AFNetworking是一个非常方便的网络请求库,可以轻松实现各种网络请求,比如经常使用的GET请求、POST请求,以及上传多张图片等,下面介绍对AFNetworking简单封装GET,POST请求。
1 调用
- (void)test
{
NSString *url = @""; //请求的url
NSDictionary *params = @{@"key":@"value"}; //请求的参数
[PSJWebRequestManager asynRequestWithUrl:url
method:PSJRequestGet //设置post或get
params:params
finish:^(PSJResult *vResult) {
NSLog(@"错误码:%ld 错误信息:%@ 返回响应内容:%@",(long)vResult.error.errorCode,vResult.error.description,vResult.result);
} fail:^(PSJResult *vResult) {
}];
}
2 封装GET,POST请求,block回调响应结果。
- (void)requestWithUrl:(NSString *)url method:(PSJRequestMethods)method params:(id)params
{
// NSString *requstUrl = [NSString stringWithFormat:@"%@%@",GM_HOST_URL,url];
NSString *requstUrl = [NSString stringWithFormat:@"%@/%@",[self psj_hostUrl],
url];
NSMutableDictionary *mParams = [NSMutableDictionary dictionary];
if ([params isKindOfClass:[NSDictionary class]]) {
if (params != nil) {
[mParams addEntriesFromDictionary:params];
}
}
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain",@"application/json",nil];
manager.requestSerializer.timeoutInterval = [self psj_timeOutInter];
if (method == PSJRequestGet) {
NSString *url_get = [self fitUrl:requstUrl params:mParams];
[manager GET:url_get parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
PSJResult *vresult = [[PSJResult alloc] init];
vresult.result = responseObject;
vresult.error = [self errorWithDict:vresult.result];
if (vresult.result == nil) {
self.failedBlock(vresult);
}
else{
self.finishBlock(vresult);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
PSJResult *vresult = [[PSJResult alloc] init];
vresult.error = [PSJError errorWithNSError:error];
self.failedBlock(vresult);
}];
}
else if (method == PSJRequestPost) {
requstUrl = [requstUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[manager POST:requstUrl parameters:mParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
PSJResult *vresult = [[PSJResult alloc] init];
vresult.result = responseObject;
vresult.error = [self errorWithDict:vresult.result];
if (vresult.result == nil) {
self.failedBlock(vresult);
}
else{
self.finishBlock(vresult);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
PSJResult *vresult = [[PSJResult alloc] init];
vresult.error = [PSJError errorWithNSError:error];
self.failedBlock(vresult);
}];
}
}
3 PSJResult响应结果封装类
@interface PSJResult : NSObject
/**
* 请求结果
*/
@property (nonatomic,strong) id result;
/**
* 请求错误
*/
@property (nonatomic,strong) PSJError *error;
@end
/*!
@class
@brief SDK错误信息定义类
@discussion
*/
@interface PSJError : NSObject
/*!
@property
@brief 错误代码
*/
@property (nonatomic) GMErrorType errorCode;
/*!
@property
@brief 错误信息描述
*/
@property (nonatomic, copy) NSString *description;
/*!
@method
@brief 创建一个EMError实例对象
@param errCode 错误代码
@param description 错误描述信息
@discussion
@result 错误信息描述实例对象
*/
+ (PSJError *)errorWithCode:(GMErrorType)errCode
andDescription:(NSString *)description;
/*!
@method
@brief 通过NSError对象, 生成一个GMError对象
@param error NSError对象
@discussion
@result 错误信息描述实例对象
*/
+ (PSJError *)errorWithNSError:(NSError *)error;
@end
DEMO地址: http://download.csdn.net/detail/jueyi1127/9616128