网络请求封装_html/css_WEB-ITnose

WBOY
Release: 2016-06-21 08:47:00
Original
1564 people have browsed it

新建NetTool文件继承于NSObject在里面进行网络请求的封装


NetTool.h

  • 定义两个枚举类型 然后重命名两个block

      //get&post用枚举来展示数据类型  typedef NS_ENUM(NSUInteger, WSSResponseStyle) {  WSSJSON,  WSSDATA,  WSSXML,  };  //POST里body类型的枚举  typedef NS_ENUM(NSUInteger, WSSRequestStyle) {  WSSBodyJSON,  WSSBodyString,  };  //重定义成功和失败的block  typedef void(^blockofSuccess)(id result);  typedef void(^blockofFailure)(NSError *error);  @interface WSSNetTool : NSObject
    Copy after login
  • 定义方法,分别是简单GET请求,简单POST请求,带加载小菊花的GET请求

      //对AFN再进行GET封装,方便自己使用  + (void)GetNetByURL:(NSString *)urlString                 body:(id)body//GET的body体              headers:(NSDictionary *)headers//请求头             response:(WSSResponseStyle)response//枚举类型              success:(blockofSuccess)success//成功之后返回这个block              failure:(blockofFailure)failure;//想要返回的数据类型(枚举类型的JSON/DATA/XML)                                              //失败之后返回这个block,可以在里面打印error了解请求失败的原因  //对AFN再进行POST封装,方便自己使用  + (void)PostNetByURL:(NSString *)urlString                 body:(id)body//POST的body体            bodyStyle:(WSSRequestStyle)bodyStyle//body类型(枚举值)              headers:(NSDictionary *)headers//请求头             response:(WSSResponseStyle)response              success:(blockofSuccess)success              failure:(blockofFailure)failure;//想要返回的数据类型(枚举类型的JSON/DATA/XML)  //有小菊花的get封装  + (void)GetNet2ByURL:(NSString *)urlString                 body:(id)body//GET的body体              headers:(NSDictionary *)headers//请求头             response:(WSSResponseStyle)response              success:(blockofSuccess)success              failure:(blockofFailure)failure             selfView:(UIView *)selfView;
    Copy after login

NetTool.m

  • 简单GET请求的实现
+(void)GetNetByURL:(NSString *)urlString body:(id)body headers:(NSDictionary *)headers response:(WSSResponseStyle)response success:(blockofSuccess)success failure:(blockofFailure)failure{    //1.获取网络管理者    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];    //2.请求头的设置    if (headers)//如果有请求头    {        for (NSString *key in headers.allKeys)        {            [manager.requestSerializer setValue:headers[key] forHTTPHeaderField:key];        }    }    //3.设置返回数据的类型    switch (response)    {        case WSSJSON:            manager.responseSerializer = [AFJSONResponseSerializer serializer];            break;        case WSSXML:            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];            break;        case WSSDATA:            manager.responseSerializer = [AFHTTPResponseSerializer serializer];            break;        default:            break;    }    //4.设置响应数据的类型    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];    //5.若有汉语进行UTF8转码(这个方法是iOS9之后的方法)    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];    //6.发送请求    [manager GET:urlString parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {        //成功block回调        success(responseObject);    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {        NSLog(@"失败原因:%@", error);        //失败block回调        failure(error);    }];}
Copy after login
  • 简单POST请求的实现
+ (void)PostNetByURL:(NSString *)urlString body:(id)body bodyStyle:(WSSRequestStyle)bodyStyle headers:(NSDictionary *)headers response:(WSSResponseStyle)response success:(blockofSuccess)success failure:(blockofFailure)failure{    //1.获取网络管理者    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];    //2.设置body数据类型    switch (bodyStyle) {        case WSSBodyJSON:            manager.requestSerializer = [AFJSONRequestSerializer serializer];            break;        case WSSBodyString:            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {                return parameters;            }];            break;        default:            break;    }    //3.请求头的设置    if (headers)//如果有请求头    {        for (NSString *key in headers.allKeys)        {            [manager.requestSerializer setValue:headers[key] forHTTPHeaderField:key];        }    }    //4.设置返回数据的类型    switch (response) {        case WSSJSON:            manager.responseSerializer = [AFJSONResponseSerializer serializer];            break;        case WSSXML:            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];            break;        case WSSDATA:            manager.responseSerializer = [AFHTTPResponseSerializer serializer];            break;        default:            break;    }    //5.设置响应数据的类型    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];    //设置可请求https    manager.securityPolicy.allowInvalidCertificates = YES;    //6.若有汉语进行UTF8转码(这个方法是iOS9之后的方法)    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];    //7.发送请求    [manager POST:urlString parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {        //成功block回调        success(responseObject);    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {        NSLog(@"失败原因:%@", error);        //失败block回调        failure(error);    }];}
Copy after login
  • 带有加载小菊花的GET请求的实现ps:这个方法的参数多了一个self.view是为了实现加载小菊花的添加以及加载结束小菊花的remove
+ (void)GetNet2ByURL:(NSString *)urlString body:(id)body headers:(NSDictionary *)headers response:(WSSResponseStyle)response success:(blockofSuccess)success failure:(blockofFailure)failure selfView:(UIView *)selfView{    //1.获取网络管理者    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];    //2.请求头的设置    if (headers)//如果有请求头    {        for (NSString *key in headers.allKeys)        {            [manager.requestSerializer setValue:headers[key] forHTTPHeaderField:key];        }    }    //3.设置返回数据的类型    switch (response)    {        case WSSJSON:            manager.responseSerializer = [AFJSONResponseSerializer serializer];            break;        case WSSXML:            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];            break;        case WSSDATA:            manager.responseSerializer = [AFHTTPResponseSerializer serializer];            break;        default:            break;    }    //4.设置响应数据的类型    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];    //5.若有汉语进行UTF8转码(这个方法是iOS9之后的方法)    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];    //添加菊花    MBProgressHUD *mbView = [MBProgressHUD showHUDAddedTo:selfView animated:YES];    mbView.detailsLabelText = @"请耐心等待,网络加载中";    //6.发送请求    [manager GET:urlString parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {        //成功block回调        success(responseObject);        //取消菊花        [mbView hide:YES];    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {        NSLog(@"失败原因:%@", error);        //取消菊花        [mbView hide:YES];        //失败block回调        failure(error);    }];}
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template