衆所周知,蘋果搞的一套框架NSContention發送請求與接收請求的方式十分繁瑣。操作起來很不方便。不僅要做區分各種請求設置各種不同的參數,而且還要經常在多線程裏操作,同時還要對請求與返回的數據做各種序列化的操作,同時還要考慮請求數據的安全等一堆問題。
AFNetworking的出現:MAC/iOS設計的一套網絡框架.(爲了簡化網絡操作)
二、AFNetworking的使用
關於修改AFN源碼:通常序列化時做對text/plan等的支持時,可以一勞永逸的修改源代碼,在acceptableContentTypes中修改即可。
AFN進行GET、POST登錄:
#pragma mark - get/post登錄
- (void)getLogin {
//1.管理器
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//2.設置登錄參數
NSDictionary *dict = @{ @"username":@"xn", @"password":@"123" };
//3.請求
[manager GET:@"http://localhost/login.php" parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"GET --> %@, %@", responseObject, [NSThread currentThread]); //自動返回主線程
} failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
}
/**
* 和上面的GET用法完全一樣, 只有一個POST參數不一樣
*/
- (void)postLogin {
//1.管理器
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//2.設置登錄參數
NSDictionary *dict = @{ @"username":@"xn", @"password":@"123" };
//3.請求
[manager POST:@"http://localhost/login.php" parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"POST --> %@, %@", responseObject, [NSThread currentThread]); //自動返回主線程
} failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
}
AFN進行網絡數據解析,獲取Plist,JSON,XML(AFN不支持自動解析XML,有專門的框架去做,如SAX,PULL,KissXML等)
#pragma mark - get 數據解析
- (void)getJSON {
//1.請求管理器
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//2.發起請求
[manager GET:@"http://localhost/videos.json" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
}
/**
* 不支持XML數據解析
*/
- (void)getXML {
//1.管理器
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//2.設置返回數據類型
manager.responseSerializer = [AFXMLParserResponseSerializer serializer]; //先實例化一下
//3.發起請求
[manager GET:@"http://localhost/videos.xml" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
}
- (void)getPlist {
//1.管理器
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//2.設置response類型
manager.responseSerializer = [AFPropertyListResponseSerializer serializer]; //是Response, 別寫成request了. 修改爲plist類型.
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"]; //這個可以直接往框架裏面修改.
//3.請求
[manager GET:@"http://localhost/videos.plist" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
}
用AFN來POST JSON數據,上傳、下載等。(上傳、下載主頁說明上有https://github.com/AFNetworking/AFNetworking)
#pragma mark - post json數據與上傳文件等
- (void)postJSON {
//1.管理器
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//2.設定類型. (這裏要設置request-response的類型)
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; //這個決定了下面responseObject返回的類型
// manager.responseSerializer = [AFJSONResponseSerializer serializer];
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
//2.設置登錄參數
NSDictionary *dict = @{ @"username":@"xn", @"password":@"123" };
//3.發送請求
[manager POST:@"http://localhost/postjson.php" parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) {
// NSLog(@"postjson--> %@", responseObject); //這樣顯示JSON的話需要設置text/plain
NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@",result);
} failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
}
轉載請註明出處:http://blog.csdn.net/xn4545945