封装AFNetworking
用了一下AFNetworking感觉比ASIHttprequest 真心好用一些,因为我还是个初学者吧,很多ASIHttprequest 的功能还没有用到,与ASIHttprequest 不用的是AFNetworking不用写代理回调函数啦,直接用的__block,写起来会简练很多
仿照AFNetworking写了一个POST的,用__block做回调函数,以后关于AFNetworking的就一直放在这里吧
引用#import "AFNetworking.h"
传输格式Json,输入字典,输出字典(不过AFNetworking本来就是字典输入输出啦),基本只是改了一下传输格式而已呵呵
1 +(void)PostDataFromNet:(NSString *)urlstring InputParas:(NSDictionary *)inputParas success:(void(^)(NSDictionary * resultDic))success failure:(void(^)(NSError *error))failure{ 2 AFHTTPRequestOperationManager *requestMange=[AFHTTPRequestOperationManager manager]; 3 requestMange.requestSerializer=[AFJSONRequestSerializer serializer];//这里设置输入格式 4 [requestMange POST:urlstring parameters:inputParas success:^(AFHTTPRequestOperation *operation, id responseObject) { 5 if (success) { 6 success(responseObject); 7 } 8 9 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 10 failure(error); 11 }]; 12 13 }
AFNetWorking的Get用法,只需要将POST方法改成GET,然后parameters设置为nil
1 +(void)GetDataFromNet:(NSString *)urlstring success:(void (^)(NSDictionary *))success failure:(void (^)(NSError *))failure{ 2 AFHTTPRequestOperationManager *requestManger=[AFHTTPRequestOperationManager manager]; 3 [requestManger GET:urlstring parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 4 if (success) { 5 success(responseObject); 6 } 7 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 8 failure(error); 9 }]; 10 }
作者:KeithMorning
出处:http://www.cnblogs.com/keithmoring/
关于作者:欢迎转载,且在文章页面明显位置给出原文链接
如有问题,可以通过keith@keithmorning.com 联系我,非常感谢。