關于修改AFN源碼:通常序列化時做對text/plan等的支持時,可以一勞永逸的修改源代碼,在acceptableContentTypes中修改即可。
AFN進行GET、POST登錄:
[objc] view plaincopy#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等)[objc] view plaincopy#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)[objc] view plaincopy#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新聞熱點
疑難解答