用了一下AFNetworking感受比ASIHttprequest 真心好用一些,由於我仍是個初學者吧,不少ASIHttprequest 的功能尚未用到,與ASIHttprequest 不用的是AFNetworking不用寫代理回調函數啦,直接用的__block,寫起來會簡練不少函數
仿照AFNetworking寫了一個POST的,用__block作回調函數,之後關於AFNetworking的就一直放在這裏吧url
引用#import "AFNetworking.h"spa
傳輸格式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設置爲nilcode
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 }