关于块语法的封装


1,在.h中通过协议把方法封装

#import <Foundation/Foundation.h>


typedef void (^CnnBlock)(NSMutableData *data);


@interface Connection : NSObject<NSURLConnectionDataDelegate>

{

    NSMutableData *_receiveData;

}

@property (nonatomic, copy) CnnBlock connectBlock;


+ (void)connectGetWithUrl:(NSString *)urlStr block:(CnnBlock)block;

+ (void)connectPostWithUrl:(NSString *)urlStr httpBody:(NSString *)httpBody block:(CnnBlock)block;


@end

2.在.m中实现

//

//  Connection.m

//  COC05

//

//  Created by 杰帝 on 14-6-24.

//  Copyright (c) 2014 杰帝. All rights reserved.

//


#import "Connection.h"


@implementation Connection


- (void)dealloc

{

    Block_release(_connectBlock);

    [super dealloc];

}


// 根据传入的网址进行网络请求

+ (void)connectGetWithUrl:(NSString *)urlStr block:(CnnBlock)block{

    Connection *conn = [[Connection alloc] init];

    conn.connectBlock = block;

    [conn startGetWithUrl:urlStr];

    [conn release];

}

+ (void)connectPostWithUrl:(NSString *)urlStr httpBody:(NSString *)httpBody block:(CnnBlock)block{

    Connection *conn = [[Connection alloc] init];

    conn.connectBlock = block;

    [conn startPostWithUrl:urlStr httpBody:httpBody];

    [conn release];

}


// 初始化网络类对象

- (instancetype)init

{

    self = [super init];

    if (self) {

        _receiveData = [[NSMutableData alloc] init];

    }

    return self;

}


// 开始POST请求

- (void)startPostWithUrl:(NSString *)urlStr httpBody:(NSString *)httpBody{

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];

    [request setHTTPMethod:@"POST"]; // 设置请求方式

    

    NSData *valueData = [httpBody dataUsingEncoding:NSUTF8StringEncoding];

    [request setHTTPBody:valueData];

    

    [NSURLConnection connectionWithRequest:request delegate:self];

}


// 开始GET请求

- (void)startGetWithUrl:(NSString *)url{

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];

    request.HTTPMethod = @"GET";

    

    [NSURLConnection connectionWithRequest:request delegate:self];

    

}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    [_receiveData setData:nil];

//    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

//    NSDictionary *dic = [httpResponse allHeaderFields];

//    NSLog(@"dic == %@",dic);

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    NSLog(@"拼接数据");

    [_receiveData appendData:data];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSLog(@"数据接收完毕");

    self.connectBlock(_receiveData);

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

    if (!error) {

        NSLog(@"Connect Error: %@",error);

    }

}


@end

3.在控制器里调用以上解析
post:

  [Connection connectPostWithUrl:@"http://appcms.duowan.com/?r=api/GetArticleListByTags" httpBody:[NSString stringWithFormat:@"lastId=%@&channel=coc&tags=新闻",[MyFMDB selectLastIDWithTags:@"新闻"]] block:^(NSMutableData *data) {

        [self jsonParser:data tableName:@"news" tags:@"新闻"];

    }];

get:

 NSString *str = [NSString stringWithFormat:@"http://appcms.duowan.com/?r=api/GetArticleDetail&url=http://coc.duowan.com/1406/%@.html", self.currentValue];

   // NSURL *url =[NSURL URLWithString: str];

    [Connection connectGetWithUrl:str block:^(NSMutableData *data) {

        [self jsonParser:data];

    }];

4.通过解析就能的到我们想要的值

  NSError *error = nil;

  _dic = [NSJSONSerialization JSONObjectWithData:sender options:NSJSONReadingMutableContainers error:&error];





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值