zoukankan      html  css  js  c++  java
  • AFNetworking 简单应用

    最近最学习 AFNetworking ,根据自己所学对 AFNetWorking 一些简单应用做了一下简单封装,主要有 get,post形式获取 xml,json,get 方式获取图片,下载文件,上传文件,代码如下:

     1 //
     2 //  AFNetWorking_Demo.h
     3 //  AFNetWorking_Demo
     4 //
     5 //  Created by Ager on 15/11/4.
     6 //  Copyright © 2015年 Ager. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 
    11 @interface AFNetWorking_Demo : NSObject
    12 
    13 
    14 /**
    15  *  get 方法请求 xml
    16  *  block success 参数类型 NSXMLParser
    17  */
    18 + (void)requestXML_GET:(NSString*)strUrl success:(void(^)(id XMLParser))success fail:(void(^)(id error))fail;
    19 
    20 /**
    21  *  post 方法请求 xml
    22  *  block success 参数类型 NSXMLParser
    23  */
    24 + (void)requestXML_POST:(NSString*)strUrl parameters:(NSString*)parameters success:(void(^)(id XMLParser))success fail:(void(^)(id error))fail;
    25 
    26 /**
    27  *  get 方法请求 JSON
    28  *  block success 参数类型为解析好的 JSON 数据 , 字典或数组
    29  */
    30 + (void)requestJSON_GET:(NSString*)strUrl success:(void(^)(id json))success fail:(void(^)(id error))fail;
    31 
    32 /**
    33 *  post 方法请求 JSON
    34 *  block success 参数类型为解析好的 JSON 数据 , 字典或数组 
    35 */
    36 + (void)requestJSON_POST:(NSString*)strUrl parameters:(NSDictionary*)parameters success:(void(^)(id json))success fail:(void(^)(id error))fail;
    37 
    38 
    39 /**
    40  *  get 方法请求 img
    41  */
    42 + (void)requestIMG_GET:(NSString*)strUrl success:(void(^)(id image))success fail:(void(^)(id error))fail;
    43 
    44 /**
    45  *  下载文件
    46  *
    47  *  @param urlStr  要下载文件的路径
    48  */
    49 + (void)sessionDownloadWithUrl:(NSString *)urlStr success:(void (^)(NSURL *fileURL))success fail:(void (^)())fail;
    50 
    51 /**
    52  *  上传文件
    53  *
    54  *  @param urlStr   上传网址地址
    55  *  @param filePath 要上传的文件沙盒路径
    56  */
    57 + (void)upLoad:(NSString*)urlStr filePath:(NSString *)filePath success:(void(^)(id responseObject))success fail:(void(^)())fail;
    58 
    59 @end
    AFNetWorking_Demo.h
      1 //
      2 //  AFNetWorking_Demo.m
      3 //  AFNetWorking_Demo
      4 //
      5 //  Created by Ager on 15/11/4.
      6 //  Copyright © 2015年 Ager. All rights reserved.
      7 //
      8 
      9 #import "AFNetWorking_Demo.h"
     10 #import "AFNetworking.h"
     11 
     12 @implementation AFNetWorking_Demo
     13 
     14 
     15 /**
     16  *  get 方法请求 xml
     17  */
     18 + (void)requestXML_GET:(NSString*)strUrl success:(void(^)(id XMLParser))success fail:(void(^)(id error))fail{
     19     
     20     AFHTTPRequestOperationManager *mamager = [AFHTTPRequestOperationManager manager];
     21     
     22     mamager.responseSerializer = [[AFXMLParserResponseSerializer alloc]init];
     23     
     24     [mamager GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
     25         
     26         success(responseObject);
     27         
     28     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     29         
     30         fail(error);
     31         
     32     }];
     33     
     34 }
     35 
     36 
     37 
     38 /**
     39  *  post 方法请求 xml
     40  *
     41  * parameters 为 JSON 数据类型
     42  */
     43 + (void)requestXML_POST:(NSString*)strUrl parameters:(NSString*)parameters success:(void(^)(id XMLParser))success fail:(void(^)(id error))fail{
     44     
     45     AFHTTPRequestOperationManager *maneger = [AFHTTPRequestOperationManager manager];
     46     //设置请求格式
     47     maneger.requestSerializer = [[AFJSONRequestSerializer alloc]init];
     48     //设置返回数据格式
     49     maneger.responseSerializer = [[AFXMLParserResponseSerializer alloc]init];
     50     
     51     
     52     [maneger POST:strUrl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
     53         success(responseObject);
     54     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     55         fail(error);
     56     }];
     57 }
     58 
     59 /**
     60  *  get 方法请求 json
     61  */
     62 + (void)requestJSON_GET:(NSString*)strUrl success:(void(^)(id json))success fail:(void(^)(id error))fail{
     63     
     64     AFHTTPRequestOperationManager *mamager = [AFHTTPRequestOperationManager manager];
     65     
     66     mamager.responseSerializer = [[AFJSONResponseSerializer alloc]init];
     67     
     68     mamager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/plain",@"text/html",nil];
     69     
     70     [mamager GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
     71         
     72         success(responseObject);
     73         
     74     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     75         
     76         fail(error);
     77         
     78     }];
     79     
     80 }
     81 
     82 
     83 
     84 
     85 + (void)requestIMG_GET:(NSString*)strUrl success:(void(^)(id image))success fail:(void(^)(id error))fail{
     86     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
     87     
     88     manager.responseSerializer = [[AFImageResponseSerializer alloc]init];
     89     
     90     [manager GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
     91         if (success) {
     92             success(responseObject);
     93         }
     94     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     95         if (fail) {
     96             fail(error);
     97         }
     98     }];
     99 }
    100 
    101 /**
    102  *  文件下载
    103  */
    104 + (void)sessionDownloadWithUrl:(NSString *)urlStr success:(void (^)(NSURL *fileURL))success fail:(void (^)())fail
    105 {
    106     
    107     AFURLSessionManager *manage = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    108     
    109     //设置 request
    110     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    111        //设置下载任务
    112     NSURLSessionDownloadTask *tast = [manage downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    113         //指定文件下载路径
    114         //将下载文件保存在缓存路径中
    115         
    116         NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    117         NSString *filePath = [path stringByAppendingString:response.suggestedFilename];
    118         
    119         // URLWithString返回的是网络的URL,如果使用本地URL,需要注意
    120         NSURL *url = [NSURL fileURLWithPath:filePath];
    121         
    122         if (success) {
    123             success(url);
    124         }
    125         return url;
    126         
    127     } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    128         NSLog(@"%@---- %@", filePath, error);
    129         if (error) {
    130             fail();
    131         }
    132     }];
    133     
    134     [tast resume];
    135 }
    136 
    137 /**
    138  *  文件上传
    139  *  block success 参数类型为解析好的 JSON 数据 , 字典或数组 
    140  */
    141 + (void)upLoad:(NSString*)urlStr filePath:(NSString *)filePath success:(void(^)(id responseObject))success fail:(void(^)())fail{
    142     
    143     AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    144      NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    145     
    146     NSURLSessionUploadTask *task = [manager uploadTaskWithRequest:request fromFile:[NSURL fileURLWithPath:filePath] progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    147         if (error) {
    148             fail();
    149         }else{
    150             success(responseObject);
    151         }
    152     }];
    153     [task resume];
    154     
    155     
    156 }
    157 
    158 /**
    159  *  POST - 请求JSON
    160  *  需要导入 UIKit+AFNetworking
    161  */
    162 + (void)requestJSON_POST:(NSString*)strUrl parameters:(NSDictionary*)parameters success:(void(^)(id json))success fail:(void(^)(id error))fail{
    163     
    164     AFHTTPRequestOperationManager *maneger = [AFHTTPRequestOperationManager manager];
    165     //设置请求格式
    166     maneger.requestSerializer = [[AFHTTPRequestSerializer alloc]init];
    167     //设置返回数据格式
    168     maneger.responseSerializer = [[AFJSONResponseSerializer alloc]init];
    169    
    170     maneger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/plain",@"text/html",nil];
    171     
    172     [maneger POST:strUrl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    173         success(responseObject);
    174     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    175         fail(error);
    176     }];
    177 }
    178 
    179 
    180 @end
    AFNetWorking_Demo.m

    AFNetworking-2.6.1.zip 下载

  • 相关阅读:
    【leetcode】1215.Stepping Numbers
    【leetcode】1214.Two Sum BSTs
    【leetcode】1213.Intersection of Three Sorted Arrays
    【leetcode】1210. Minimum Moves to Reach Target with Rotations
    【leetcode】1209. Remove All Adjacent Duplicates in String II
    【leetcode】1208. Get Equal Substrings Within Budget
    【leetcode】1207. Unique Number of Occurrences
    【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays
    【leetcode】LCP 3. Programmable Robot
    【leetcode】LCP 1. Guess Numbers
  • 原文地址:https://www.cnblogs.com/Ager/p/4941115.html
Copyright © 2011-2022 走看看