简单的网络请求封装

NetWorkHelper.h

#import <Foundation/Foundation.h>

@protocol NetWorkHelperDelegate <NSObject>

@required
//参数为所需要传出去的值(解析好的)
- (void)passValueWithData: (id)value;

@end

@interface NetWorkHelper : NSObject

@property (nonatomic,assign) id<NetWorkHelperDelegate> delegate;

//同步get请求
- (void)getAndSynchronousMethodWithURL: (NSString *)urlString;
//同步post请求
- (void)postAndSynchronousMethodWithURL: (NSString *)urlString;

//异步post请求
- (void)postAndAsynchronousMethodWithUrl: (NSString *)urlString;
//异步get
- (void)getAndAsynchronousMethodWithURL: (NSString *)urlString;

@end

NetWorkHelper.m

#import "NetWorkHelper.h"

@implementation NetWorkHelper

- (void)jsonParserWithData: (NSData *)data
{
    if (data) {
        id value = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        [self.delegate passValueWithData:value];
    }
    
}

//同步get请求
- (void)getAndSynchronousMethodWithURL: (NSString *)urlString
{
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSError *error;
    NSData *receiveData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    [self jsonParserWithData:receiveData];
    
}

//异步POST请求
- (void)postAndAsynchronousMethodWithUrl: (NSString *)urlString
{
    NSArray *array = [urlString componentsSeparatedByString:@"?"];
    NSURL *url = [NSURL URLWithString:array[0]];
    NSData *postData = [array[1] dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self jsonParserWithData:data];
    }];
    
}

//同步post
- (void)postAndSynchronousMethodWithURL: (NSString *)urlString
{
    NSArray *array = [urlString componentsSeparatedByString:@"?"];
    NSURL *url = [NSURL URLWithString:array[0]];
    //创建POST请求参数,为NSData类型
    NSString *postString = array[1];
    //将string类型转换为NSData类型
    NSData *postParameterData = [postString dataUsingEncoding:NSUTF8StringEncoding];
    //创建请求,因为NSURLRequest类型不能设置请求方式,所以如果是post请求,就得使用它的子类NSMutableURLRequest
    NSMutableURLRequest *mutableReq = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    mutableReq.HTTPMethod = @"POST";
    //设置请求参数
    mutableReq.HTTPBody = postParameterData;
    
    //建立同步连接
    NSData *receiveData = [NSURLConnection sendSynchronousRequest:mutableReq returningResponse:nil error:nil];
    [self jsonParserWithData:receiveData];
    
}

//异步get
- (void)getAndAsynchronousMethodWithURL: (NSString *)urlString
{
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self jsonParserWithData:data];
    }];
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容