轉自:http://blog.csdn.net/kuro2007/article/details/8945323
1.下載AFNetworking資源包 https://github.com/AFNetworking/AFNetworking。
2.將資源包添加到工程文件。
3.在工程的Supporting File羣組中打開預編譯頭文件XXX-Prefix.pch。然後在別的import後面添加如下一行代碼#import “AFNetworking”
將AFNetworking添加到預編譯頭文件,意味着這個框架會被自動的添加到工程的所有源代碼文件中。
4.AFNetworking通過網絡來加載和處理結構化的數據非常明智,它支持JSON,XML,Property List。
- static NSString*const BaseURLString = @"http://www.raywenderlich.com/downloads/weather_sample/";
- // 1
- NSString *weatherUrl = [NSStringstringWithFormat:@"%@weather.php?format=json",BaseURLString];
- NSURL *url = [NSURLURLWithString:weatherUrl];
- NSURLRequest *request = [NSURLRequestrequestWithURL:url];
- // 2
- AFJSONRequestOperation *operation =
- [AFJSONRequestOperationJSONRequestOperationWithRequest:request
- success:^(NSURLRequest*request, NSHTTPURLResponse *response, id JSON) {
- //
- NSDictionary*dicWeather = (NSDictionary *)JSON;
- NSLog(@"result:%@",dicWeather);
- }
- failure:^(NSURLRequest*request, NSHTTPURLResponse *response, NSError *error, id JSON) {
- UIAlertView*alertView = [[UIAlertView alloc] initWithTitle:@"Error RetrievingWeather"
- message:[NSStringstringWithFormat:@"%@",error]
- delegate:self
- cancelButtonTitle:@"OK"
- otherButtonTitles: nil];
- [alertView show];
- }];
- // 5
- [operation start];
(1)根據基本的URL構造除完整的一個URL,然後通過這個完整的URL獲得一個NSURL對象,然後根據這個url獲得一個NSURLRequest。
(2)AFJSONRequestOperation是一個完整的類,整合了從網絡中獲取數據並對JSON進行解析。
(3)當請求成功,則運行成功塊。在本例中,把解析出來的天氣數據從JSON變量轉換爲一個字典(dictionary),並將其存儲在字典中。
(4)如果運行出問題了,則運行失敗塊(failure block),比如網絡不可用。如果failure block被調用了,將會通過提示框顯示錯誤信息。
6.AFNetWorking異步加載圖片
- (1)#import “UIImageView+AFNetworking.h”
- (2)UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(40, 80, 40, 40)];
- __weak UIImageView *_imageView = imageView;
- [imageViewsetImageWithURLRequest:[[NSURLRequest alloc] initWithURL:[NSURLURLWithString:@"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"]]
- placeholderImage:[UIImage imageNamed:@"placeholder.png"]
- success:^(NSURLRequest *request,NSHTTPURLResponse *response, UIImage *image) {
- _imageView.image = image;
- [_imageView setNeedsDisplay];
- }
- failure:^(NSURLRequest *request, NSHTTPURLResponse*response, NSError *error) {
- ;
- }];
- [self.view addSubview:imageView];
7.GET 和POST請求
(1).構建一個baseURL,以及一個參數字典,並將這兩個變量傳給AFHTTPClient.
(2).將AFJSONRequestOperation註冊爲HTTP的操作, 這樣就可以跟之前的示例一樣,可以獲得解析好的JSON數據。
(3).做了一個GET請求,這個請求有一對block:success和failure。
(4).POST請求跟GET一樣
- AFHTTPClient *client= [[AFHTTPClient alloc] initWithBaseURL:baseURL];
- [clientregisterHTTPOperationClass:[AFJSONRequestOperation class]];
- [clientsetDefaultHeader:@"Accept" value:@"application/json"];
- [client postPath:@"weather.php"
- parameters:parameters
- success:^(AFHTTPRequestOperation *operation, id responseObject) {
- self.weather =responseObject;
- self.title = @"HTTPPOST";
- [self.tableViewreloadData];
- }
- failure:^(AFHTTPRequestOperation *operation, NSError*error) {
- UIAlertView *av =[[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
- message:[NSStringstringWithFormat:@"%@",error]
- delegate:nil
- cancelButtonTitle:@"OK" otherButtonTitles:nil];
- [av show];
- }
- ];
- [client getPath:@"weather.php"
- parameters:parameters
- success:^(AFHTTPRequestOperation *operation, id responseObject) {
- self.weather =responseObject;
- self.title = @"HTTP GET";
- [self.tableViewreloadData];
- }
- failure:^(AFHTTPRequestOperation *operation, NSError*error) {
- UIAlertView *av =[[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
- message:[NSStringstringWithFormat:@"%@",error]
- delegate:nil
- cancelButtonTitle:@"OK" otherButtonTitles:nil];
- [av show];
- }
- ];
8.AFNetworking更新背景圖片
Note:
AFJSONOperation,AFPropertyListOperation, AFXMLOperation用來解析結構化數據。
UIImageView+AFNetworking用來快捷的填充image view
AFHTTPClient用來進行更底層的請求
用自定義的AFHTTPClient子類來訪問一個web service。
AFNetworkActivityIndicatiorManager用來給用戶做出網絡訪問的提示。
AFImageRequestOperation用來加載圖片。