首页 > 代码库 > AFNetWorking封装请求数据
AFNetWorking封装请求数据
//
// YXYAppDelegate.m
// AFNetWorking封装
//
// Created by lanjiying on 14/11/20.
// Copyright (c) 2014年 lanjiying. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "AFNetworking.h"
typedefvoid(^CompletionLoad)(NSObject *result);
@interface YXYDataService :NSObject
///如果请求中没有请求头,使用此方法,如果需要传图片则需要将图片封装成NSDate,装到params中,url为地址,params为请求体没有传nil,get和post方法可以忽略大小写
+(AFHTTPRequestOperation *)requestWithURL:(NSString *)url params:(NSMutableDictionary *)params httpMethod:(NSString *)httpMethod block:(CompletionLoad)block;
///url为请求地址,params是请求体,传字典进去,,httpMethod是请求方式,block是请求完成做得工作,header是请求头,也是传字典过去(发送请求获得json数据),如果没有则传nil,如果只有value而没有key,则key可以设置为anykey(但是此方法暂时没设置传图片)
+ (AFHTTPRequestOperation *)requestWithURL:(NSString *)url
requestHeader:(NSDictionary *)header
params:(NSMutableDictionary *)params
httpMethod:(NSString *)httpMethod
block:(CompletionLoad)block
;
//
// YXYAppDelegate.m
// AFNetWorking封装
//
// Created by liujianjian on 14/11/20.
// Copyright (c) 2014年 lanjiying. All rights reserved.
//
#import "YXYDataService.h"
@implementation YXYDataService
//
+(AFHTTPRequestOperation *)requestWithURL:(NSString *)url params:(NSMutableDictionary *)params httpMethod:(NSString *)httpMethod block:(CompletionLoad)block
{
//创建request请求管理对象
AFHTTPRequestOperationManager * manager =[AFHTTPRequestOperationManagermanager];
AFHTTPRequestOperation * operation =nil;
//GET请求
NSComparisonResult comparison1 = [httpMethod caseInsensitiveCompare:@"GET"];
if (comparison1 == NSOrderedSame) {
operation =[managerGET:url parameters:paramssuccess:^(AFHTTPRequestOperation *operation,id responseObject) {
block(responseObject);
}failure:^(AFHTTPRequestOperation *operation,NSError *error) {
block(error);
}];
}
//POST请求
NSComparisonResult comparisonResult2 = [httpMethodcaseInsensitiveCompare:@"POST"];
if (comparisonResult2 == NSOrderedSame)
{
//标示
BOOL isFile = NO;
for (NSString * keyin params.allKeys)
{
id value = http://www.mamicode.com/params[key];
//判断请求参数是否是文件数据
if ([value isKindOfClass:[NSDataclass]]) {
isFile =YES;
break;
}
}
if (!isFile) {
//参数中没有文件,则使用简单的post请求
operation =[managerPOST:url
parameters:params
success:^(AFHTTPRequestOperation *operation,id responseObject) {
if (block != nil) {
block(responseObject);
}
}failure:^(AFHTTPRequestOperation *operation,NSError *error) {
if (block != nil) {
//
}
}];
}else
{
operation =[managerPOST:url
parameters:params
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
for (NSString *keyin params) {
id value = http://www.mamicode.com/params[key];
if ([value isKindOfClass:[NSDataclass]]) {
[formDataappendPartWithFileData:value
name:key
fileName:key
mimeType:@"image/jpeg"];
}
}
}success:^(AFHTTPRequestOperation *operation,id responseObject) {
block(responseObject);
}failure:^(AFHTTPRequestOperation *operation,NSError *error) {
NSLog(@"请求网络失败");
}];
}
}
//设置返回数据的解析方式
operation.responseSerializer =[AFJSONResponseSerializerserializerWithReadingOptions:NSJSONReadingMutableContainers];
return operation;
}
///url为请求地址,params是请求体,传字典进去,,httpMethod是请求方式,block是请求完成做得工作,header是请求头,也是传字典过去(发送请求获得json数据),如果没有则传nil,如果只有value而没有key,则key可以设置为anykey
+ (AFHTTPRequestOperation *)requestWithURL:(NSString *)url
requestHeader:(NSDictionary *)header
params:(NSMutableDictionary *)params
httpMethod:(NSString *)httpMethod
block:(CompletionLoad)block
{
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:[NSURLURLWithString:url] cachePolicy:NSURLRequestReloadIgnoringCacheDatatimeoutInterval:10];
//添加请求头
for (NSString *keyin header.allKeys) {
[requestaddValue:header[key] forHTTPHeaderField:key];
}
//get请求
NSComparisonResult compResult1 =[httpMethod caseInsensitiveCompare:@"GET"];
if (compResult1 == NSOrderedSame) {
[requestsetHTTPMethod:@"GET"];
if(params != nil)
{
//添加参数,将参数拼接在url后面
NSMutableString *paramsString = [NSMutableStringstring];
NSArray *allkeys = [params allKeys];
for (NSString *keyin allkeys) {
NSString *value = http://www.mamicode.com/[params objectForKey:key];
[paramsStringappendFormat:@"&%@=%@", key, value];
}
if (paramsString.length >0) {
[paramsString replaceCharactersInRange:NSMakeRange(0,1) withString:@"?"];
//重新设置url
[requestsetURL:[NSURLURLWithString:[url stringByAppendingString:paramsString]]];
}
}
}
//post请求
NSComparisonResult compResult2 = [httpMethod caseInsensitiveCompare:@"POST"];
if (compResult2 == NSOrderedSame) {
[requestsetHTTPMethod:@"POST"];
for (NSString *keyin params) {
[requestsetHTTPBody:params[key]];
}
}
//发送请求
AFHTTPRequestOperation *requstOperation = [[AFHTTPRequestOperationalloc] initWithRequest:request];
//设置返回数据的解析方式(这里暂时只设置了json解析)
requstOperation.responseSerializer = [AFJSONResponseSerializerserializer];
[requstOperationsetCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responseObject) {
if (block != nil) {
block(responseObject);
}
}failure:^(AFHTTPRequestOperation *operation,NSError *error) {
NSLog(@"Error: %@", [errorlocalizedDescription]);
if (block != nil) {
block(error);
}
}];
[requstOperationstart];
return requstOperation;
}
@end
AFNetWorking封装请求数据