AFNetworking use summary

  Steps for usage:

  1> AFNetWorking folder into the project

  2> Add MobileCoreServices.framework, SystemConfiguration.framework library

  3> Import where it is needed to use the header file "AFNetworking.h"

  4> import in Prefix.pch file (otherwise a warning)

  #import<SystemConfiguration/SystemConfiguration.h>

  #import<MobileCoreServices/MobileCoreServices.h>

  The next step is using, AFN official recommendations:

  1. Define a global AFHttpClient: comprising

      1> baseURL

      2> Request

      3> operation queue NSOperationQueue

   2. responsible for all network operations requested by AFHTTPRequestOperation

  One: Load with AFN JSON

. 1    // 1.baseURL 
2      NSString the baseURL * = @ " http://iappfree.candou.com:8080/free/applications " ;
 . 3      // 2. Create AFHTTPClient 
. 4      AFHTTPClient Client * = [[AFHTTPClient the alloc] initWithBaseURL: [ URLWithString NSURL: baseURL]];
 5      // 3. create a request request 
6      NSURLRequest * request = [Client requestWithMethod: @ " GET " path: @ " Limited's a Currency rmb = & Page = 1? " the Parameters: nil];
 7      // 4. create a connection 
8     * OP = AFJSONRequestOperation [AFJSONRequestOperation JSONRequestOperationWithRequest: Request Success: ^ (* NSURLRequest Request, Response NSHTTPURLResponse *, ID the JSON) {
 . 9          // request successfully process
 10          // direct deserialization (converted to model a collection of objects) 
. 11          NSLog ( @ " @% " , the JSON);
 12 is      } failure: ^ (* NSURLRequest request, Response NSHTTPURLResponse *, * the NSError error, ID the JSON) {
 13 is          // request failed error handling process 
14          NSLog ( @" error:% @ " , error );
 15      }];
 16      // queued, multithreaded operation 
17     [client.operationQueue addOperation:op];

 Second, the network connection status is determined using AFN

. 1  // AFNetwork according to baseUrl is able to connect to network connection status determination 
2      NSURL * URL = [NSURL URLWithString: @ " http://www.baidu.com " ];
 . 3      
. 4      AFHTTPClient Client * = [AFHTTPClient clientWithBaseURL: URL ];
 . 5      / * 
. 6       AFNetworkReachabilityStatusUnknown = -1, unknown
 . 7       AFNetworkReachabilityStatusNotReachable = 0, are not connected
 . 8       AFNetworkReachabilityStatusReachableViaWWAN =. 1, 3G
 . 9       AFNetworkReachabilityStatusReachableViaWiFi = 2, the WIFI
 10       * / 
. 11      [Client setReachabilityStatusChangeBlock: ^(AFNetworkReachabilityStatus status) {
12         switch (status) {
13             case AFNetworkReachabilityStatusUnknown:
14                 NSLog(@"未知错误");
15                 break;
16             case AFNetworkReachabilityStatusReachableViaWiFi:
17                 NSLog(@"WIFI");
18                 break;
19             case AFNetworkReachabilityStatusReachableViaWWAN:
20                 NSLog(@"3G网络");
21                 break;
22 is              Case AFNetworkReachabilityStatusNotReachable:
 23 is                  NSLog ( @ " unconnected " );
 24                  BREAK ;
 25          }
 26 is      }];

Third, the use AFN download

// 1.baseURL 
    NSString * baseURL = @ " http://musicdata.baidu.com " ;
     // 2. Create a AFHTTPClient 
    AFHTTPClient * Client = [[AFHTTPClient alloc] initWithBaseURL: [NSURL URLWithString: baseURL]]; 
    
    // 3 . establishment request 
    NSURLRequest * request = [Client requestWithMethod: @ " the GET " path: @ " DATA2 / PIC / 115 457 125 / 115457125.jpg " Parameters: nil]; 
    
    // 4. operation 
    AFHTTPRequestOperation * OP = [[AFHTTPRequestOperation the alloc] initWithRequest: Request]; 
    
    // 5. the download path settings (stored in the sandbox) 
    
    // Two Ways documents path 
#if0
     // 1> with NSSearchPathForDirectoriesInDomains 
    NSArray * docs = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString * = Documents [docs [ 0 ] stringByAppendingPathComponent: @ " Jay.jpg " ];
 #elif 1
     // 2> with 
    NSString * documents = [NSHomeDirectory () stringByAppendingPathComponent: @ " Documents / hehe.jpg " ];
 #endif 
    // specified output stream 
    op.outputStream = [NSOutputStream outputStreamToFileAtPath: the append Documents: YES]; 
    
    // set the download operation is completed
    [OP setCompletionBlockWithSuccess: ^ (AFHTTPRequestOperation * Operation, ID responseObject) { 
        NSLog ( @ " download success " ); 
    } failure: ^ (AFHTTPRequestOperation * Operation, the NSError * error) { 
        NSLog ( @ " Download failed " ); 
    }]; 
    / / set Download progress block codes 
    / * 
     number of bytes read if bytesRead 
     totalBytesRead number of bytes have been downloaded 
     totalBytesExpectedToRead total file size 
     * / 
    [OP setDownloadProgressBlock: ^ (NSUInteger bytesRead, Long  Long totalBytesRead, Long Long totalBytesExpectedToRead) { 
        CGFloat precent = ( a float ) totalBytesRead / totalBytesExpectedToRead; 
        NSLog ( @ " percentage:% G " , precent); 
    }]; 
    // Start Download 
    [client.operationQueue addOperation: op];

 

Reproduced in: https: //www.cnblogs.com/pretty-guy/p/4067391.html

Guess you like

Origin blog.csdn.net/weixin_33998125/article/details/94287724