【iOS】網絡操作與AFNetworking

衆所周知,蘋果搞的一套框架NSContention發送請求與接收請求的方式十分繁瑣。操作起來很不方便。不僅要做區分各種請求設置各種不同的參數,而且還要經常在多線程裏操作,同時還要對請求與返回的數據做各種序列化的操作,同時還要考慮請求數據的安全等一堆問題。


一、早前的幾個網絡框架
1、ASI框架HTTP終結者.很牛, 但是有BUG, 已經停止更新.
2、MKNetworkKit (印度人寫的).
3、AFN一直還在更新.

AFNetworking的出現:MAC/iOS設計的一套網絡框架.(爲了簡化網絡操作)

*AFN專注與網絡數據傳輸,以及網絡中多線程的處理.

二、AFNetworking的使用

1、AFN特性 :
*登錄傳參數時,傳遞字典即可.(鍵名爲參數名,鍵值爲參數值).
*自動到子線程中執行,執行完後返回主線程.
*返回的結果自動序列化NSDictionary.

2、使用AFN注意 :
*AFHTTPRequestOperationManager封裝了通過HTTP協議與Web應用程序進行通訊的常用方法.(這個實例化的時候不是單例, 因爲沒有shared)
*包括創建請求/響應序列化/網絡監控/數據安全.
*方法等都是AF開頭的.

3、AFN能做的 (網絡中的都涵蓋了):
*GET/POST/PUT/DELETE/HEAD請求.
*JSON數據解析/Plist數據解析.(不支持XML數據解析)
*POSTJSON.
*上傳/下載.

4、使用步驟 : (可參考說明文檔)
1.首先需要實例化一個請求管理器AFHTTPRequestOperationManager.
2.設置請求的數據格式:默認是二進制.(不是可改)
*AFHTTPRequestSerializer(二進制)
*AFJSONRequestSerializer(JSON)
*AFPropertyListRequestSerializer(Plist)
3.設置響應的數據格式:默認是JSON.(不是可改)
*AFHTTPResponseSerializer(二進制)
*AFJSONResponseSerializer(JSON)
*AFPropertyListResponseSerializer(Plist)
*AFXMLParserResponseSerializer(XML)
*AFImageResponseSerializer(Image)
*AFCompoundResponseSerializer(組合的)
4.如果響應者的MIMEType不正確,就要修改acceptableContentTypes.
5.調用方法,發送響應的請求(GET/POST...).

關於修改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,XMLAFN不支持自動解析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  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章