自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

AFNetworking使用總結(jié)

移動(dòng)開發(fā) iOS
關(guān)于AFNetworking使用總結(jié) 以及一些錯(cuò)誤的解決辦法。

AFNetworking使用總結(jié)

分享類型:游戲開發(fā)相關(guān)

1 將AFNetWorking文件夾導(dǎo)入項(xiàng)目

2 添加類庫(kù) Security.framework、MobileCoreServices.framework、SystemConfiguration.framework

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

解決編譯時(shí)警告:

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

注:AFNetWorking使用了ARC ,在不使用ARC項(xiàng)目中使用時(shí),對(duì)AFNetWorking的所有.m文件添加“-fobjc-arc” 

    在使用ARC項(xiàng)目中,使用“不使用ARC”的類庫(kù)時(shí),對(duì)類庫(kù)的.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)根據(jù)基本的URL構(gòu)造除完整的一個(gè)URL,然后通過(guò)這個(gè)完整的URL獲得一個(gè)NSURL對(duì)象,然后根據(jù)這個(gè)url獲得一個(gè)NSURLRequest。 

(2)AFJSONRequestOperation是一個(gè)完整的類,整合了從網(wǎng)絡(luò)中獲取數(shù)據(jù)并對(duì)JSON進(jìn)行解析。 

(3)當(dāng)請(qǐng)求成功,則運(yùn)行成功塊。在本例中,把解析出來(lái)的天氣數(shù)據(jù)從JSON變量轉(zhuǎn)換為一個(gè)字典(dictionary),并將其存儲(chǔ)在字典中。 

(4)如果運(yùn)行出問(wèn)題了,則運(yùn)行失敗塊(failure block),比如網(wǎng)絡(luò)不可用。如果failure block被調(diào)用了,將會(huì)通過(guò)提示框顯示錯(cuò)誤信息。

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請(qǐng)求 

(1).構(gòu)建一個(gè)baseURL,以及一個(gè)參數(shù)字典,并將這兩個(gè)變量傳給AFHTTPClient. 

(2).將AFJSONRequestOperation注冊(cè)為HTTP的操作, 這樣就可以跟之前的示例一樣,可以獲得解析好的JSON數(shù)據(jù)。 

(3).做了一個(gè)GET請(qǐng)求,這個(gè)請(qǐng)求有一對(duì)block:success和failure。 

(4).POST請(qǐng)求跟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.                 }           ];    

另外,請(qǐng)求方式可以創(chuàng)建一個(gè)類繼承AFHTTPClient ,官方的例子就是這樣寫的。 

狀態(tài)欄設(shè)置 

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

    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中添加 [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];用來(lái)給用戶做出網(wǎng)絡(luò)訪問(wèn)的提示。 

請(qǐng)求超時(shí)設(shè)置 

timeout和參數(shù)都是在NSURLRequest/NSMutableURLRequest設(shè)置的 

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

如果你是繼承了AFHTTPClient  

就需要override一個(gè)方法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; } 

這個(gè)時(shí)候的參數(shù)設(shè)置是調(diào)用 

  1. [self postPath:@"" parameters:nil //參數(shù) 
  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

責(zé)任編輯:chenqingxiang 來(lái)源: cocoachina
相關(guān)推薦

2015-08-27 09:46:09

swiftAFNetworkin

2017-04-21 16:00:09

2015-05-18 09:44:34

2015-10-22 10:32:52

AFNetworkin遷移

2021-09-06 13:15:16

golang chan技巧語(yǔ)言

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

內(nèi)網(wǎng)穿透代理工具Linux

2009-05-12 13:54:59

WEBFirebugconsole

2009-11-16 16:59:03

PHP構(gòu)造函數(shù)

2009-12-04 14:40:43

Visual Stud

2010-04-21 14:53:46

Oracle游標(biāo)

2010-11-22 16:51:10

MySQL內(nèi)存表

2009-12-04 15:43:03

PHP JSON擴(kuò)展

2010-02-02 14:06:50

C++ const變量

2010-02-06 09:59:54

C++ void使用規(guī)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)