成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

AFNetworking使用總結

移動開發 iOS
關于AFNetworking使用總結 以及一些錯誤的解決辦法。

AFNetworking使用總結

分享類型:游戲開發相關

1 將AFNetWorking文件夾導入項目

2 添加類庫 Security.framework、MobileCoreServices.framework、SystemConfiguration.framework

3 在使用的地方 #import "AFNetworking.h"

解決編譯時警告:

  1. Prefix.pch文件中加入  
  2. #import <SystemConfiguration/SystemConfiguration.h>  
  3. #import <MobileCoreServices/MobileCoreServices.h>  

注:AFNetWorking使用了ARC ,在不使用ARC項目中使用時,對AFNetWorking的所有.m文件添加“-fobjc-arc” 

    在使用ARC項目中,使用“不使用ARC”的類庫時,對類庫的.m文件添加“-fno-objc-arc”

[plain] view plaincopy

  1. static NSString*const BaseURLString = @"http://www.raywenderlich.com/downloads/weather_sample/";     
  2.     // 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. [plain] view plaincopy  
  2.   
  3. [list=1](1)#import “UIImageView+AFNetworking.h”  (2)UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(40804040)];      __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;     
  4.                               [_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一樣

[plain]view plaincopy

  1. [list=1]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];                    }           ];     
  2. [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];     
  3.                 }           ];    

另外,請求方式可以創建一個類繼承AFHTTPClient ,官方的例子就是這樣寫的。 

狀態欄設置 

  在Appdelegate里面的 - (BOOL)application:(UIApplication *)application  

    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中添加 [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];用來給用戶做出網絡訪問的提示。 

請求超時設置 

timeout和參數都是在NSURLRequest/NSMutableURLRequest設置的 

  1. [list=1
  2. NSMutableURLRequest *request = [client requestWithMethod:@"GET" path:@"/" parameters:nil];//這里的parameters:參數就是你的第二個問題如何設置參數 
  3. [request setTimeoutInterval:120]; 
  4. AFHTTPRequestOperation *operation = [client HTTPRequestOperationWithRequest:request success:^{...} failure:^{...}]; 
  5. [client enqueueHTTPRequestOperation:operation]; 

如果你是繼承了AFHTTPClient  

就需要override一個方法requestWithMethod 

  1. - (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters{    
  2. NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];    
  3. [request setTimeoutInterval:15];    
  4. return request; } 

這個時候的參數設置是調用 

  1. [self postPath:@"" parameters:nil //參數 
  2.            success:^(AFHTTPRequestOperation *operation, id responseObject) { 
  3.                if (success) { 
  4.                    success((AFJSONRequestOperation *)operation, responseObject); 
  5.                } 
  6.            } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
  7.                if (failure) { 
  8.                    failure((AFJSONRequestOperation *)operation, error); 
  9.                } 
  10.            }]; 

本文鏈接:http://www.cocoachina.com/bbs/read.php?tid=184183

責任編輯:chenqingxiang 來源: cocoachina
相關推薦

2015-08-27 09:46:09

swiftAFNetworkin

2015-05-18 09:44:34

2017-04-21 16:00:09

2015-10-22 10:32:52

AFNetworkin遷移

2021-09-06 13:15:16

golang chan技巧語言

2024-04-18 10:48:24

MongoDB

2012-09-11 16:09:04

MooseFS

2009-09-25 17:26:55

使用Hibernate

2015-08-24 08:59:13

Git技巧

2013-06-07 14:35:19

Mac OS X

2009-09-08 16:02:47

Linq使用Group

2021-11-02 10:40:51

內網穿透代理工具Linux

2009-11-16 16:59:03

PHP構造函數

2009-12-04 14:40:43

Visual Stud

2010-04-21 14:53:46

Oracle游標

2009-05-12 13:54:59

WEBFirebugconsole

2010-11-22 16:51:10

MySQL內存表

2019-10-28 14:37:10

MySQL 數據庫收藏

2015-01-07 14:49:40

AndroidStud快捷鍵

2010-02-02 14:06:50

C++ const變量
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 成人小视频在线观看 | 欧美在线一区二区三区 | 亚洲在线免费观看 | 四虎影院免费在线播放 | 亚洲一区二区电影在线观看 | 国产成人精品网站 | 亚洲精品一区二区三区中文字幕 | 欧美一区二区三区,视频 | 中文字幕精品一区二区三区精品 | 亚洲 欧美 日韩 精品 | 精品国产色 | 亚洲最色网站 | 毛色毛片免费看 | 成人免费网站www网站高清 | 精品久久久久久亚洲精品 | 国产精品美女久久久久久免费 | 一区二区中文字幕 | 欧美在线国产精品 | av在线播放不卡 | 亚洲福利av | 青青草社区 | 国产日韩一区 | 久久久久国产成人精品亚洲午夜 | 黄色av网站在线观看 | 日韩三级一区 | 国产精品视频999 | 久热精品视频 | 欧美日韩网站 | 视频一区二区中文字幕 | 欧美日韩精品 | 男女下面一进一出网站 | 国产精品中文字幕在线 | 久久久久久综合 | 久久精品99久久 | 91 在线| 中文字幕日韩欧美一区二区三区 | 久久久性色精品国产免费观看 | 午夜视频在线 | 99国内精品 | 色视频成人在线观看免 | 日日操日日干 |