用了一下AFNetworking感觉比ASIHttprequest 真心好用一些,因为我还是个初学者吧,很多ASIHttprequest 的功能还没有用到,与ASIHttprequest 不用的是AFNetworking不用写代理回调函数啦,直接用的__block,写起来会简练很多
仿照AFNetworking写了一个POST的,用__block做回调函数,以后关于AFNetworking的就一直放在这里吧
引用#import "AFNetworking.h"
传输格式Json,输入字典,输出字典(不过AFNetworking本来就是字典输入输出啦),基本只是改了一下传输格式而已呵呵
+(void)PostDataFromNet:(NSString *)urlstring InputParas:(NSDictionary *)inputParas success:(void(^)(NSDictionary * resultDic))success failure:(void(^)(NSError *error))failure{
AFHTTPRequestOperationManager *requestMange=[AFHTTPRequestOperationManager manager];
requestMange.requestSerializer=[AFJSONRequestSerializer serializer];//这里设置输入格式
[requestMange POST:urlstring parameters:inputParas success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success) {
success(responseObject);
} } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error);
}]; }
AFNetWorking的Get用法,只需要将POST方法改成GET,然后parameters设置为nil
+(void)GetDataFromNet:(NSString *)urlstring success:(void (^)(NSDictionary *))success failure:(void (^)(NSError *))failure{
AFHTTPRequestOperationManager *requestManger=[AFHTTPRequestOperationManager manager];
[requestManger GET:urlstring parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success) {
success(responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error);
}];
}