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
//
// 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
[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:@"新闻"];
}];
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];
}];
NSError *error = nil;
_dic = [NSJSONSerialization JSONObjectWithData:sender options:NSJSONReadingMutableContainers error:&error];