就是JSON解析方法封装到一个工具类中.
即传入一个网址(字符串),在使用这个工具后, 返回一个解析好的数据.
这也就是用到了协议传值或者block传值法.
// 首先是使用协议的方式
.
新建一个继承于NSObject的Model类. 名字叫做NetWorkingDelegate
然后进行协议的六步,大家应该还记得吧?!
#import <Foundation/Foundation.h>
#pragma mark 第一步: 声明一份协议
@protocol NetWorkingDelegate <NSObject>
- (void)getNetData:(id)result;
@end
@interface NetWorkingDelegate : NSObject
#pragma mark 第二步: 设置代理人属性
@property(nonatomic, assign)id<NetWorkingDelegate>delegate;
// 触发协议的方法, 减号就需要创建对象, 加号则不需要了.
// 加号方法就是在调用方法的时候只要把代理人填进去就不用再写**.delegate = **了
- (void)netWorkingWithURL:(NSString *)strURL;
+ (void)netWorkingWithURL:(NSString *)strURL delegate:(id<NetWorkingDelegate>)delegate;
@end
内部实现是这样的:
#import "NetWorkingDelegate.h"
@implementation NetWorkingDelegate
#pragma mark 设置代理人需要完成的方法.
- (void)netWorkingWithURL:(NSString *)strURL{
//因为没有办法保证网址没有中文,所以需要对传过来的网址进行转换
NSString *strENCode = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strENCode];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 数据JSON解析
id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// 已经处理好的数据让delegate带回去.
[self.delegate getNetData:result];
}];
}
+ (void)netWorkingWithURL:(NSString *)strURL delegate:(id<NetWorkingDelegate>)delegate{
NSString *strENCode = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strENCode];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 用
id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
[delegate getNetData:result];
}];
}
使用方法: 别忘了引头文件, 签订协议!
// // 创建网络工程类的对象.
// NetWorkingDelegate *netTool = [[NetWorkingDelegate alloc]init];
// // 设置代理人
// netTool.delegate = self;
// // 让对象去调用方法.
// [netTool netWorkingWithURL:@"http://api.map.baidu.com/place/v2/search?query=银行®ion=大连&output=json&ak=6E823f587c95f0148c19993539b99295"];
// 使用+方法来进行网络请求
[NetWorkingDelegate netWorkingWithURL:@"http://api.map.baidu.com/place/v2/search?query=银行®ion=大连&output=json&ak=6E823f587c95f0148c19993539b99295" delegate:self];
block
新建一个名为NetWirkingTool的Model
#import <Foundation/Foundation.h>
#pragma mark 这个类通过block的方法,把这个类的请求数据,返回到试图控制器.即block的参数就是最终结果
typedef void (^Block)(id result);
@interface NetWorkingTool : NSObject
- (void)netWorkingWithURL:(NSString *)strURL block:(Block)block;
+ (void)netWorkingWithURL:(NSString *)strURL block:(Block)block;
// 使用Block 实现POST
+ (void)NetWorkWithURL:(NSString *)strURL Body:(NSString *)bodyStr block:(Block)block;
@end
内部实现:
- (void)netWorkingWithURL:(NSString *)strURL block:(Block)block{
NSString *strENCode = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strENCode];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//把处理好的数据,通过block进行回调,返回到试图控制器
block(result);
}];
}
+ (void)netWorkingWithURL:(NSString *)strURL block:(Block)block{
NSString *strENCode = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strENCode];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//把处理好的数据,通过block进行回调,返回到试图控制器
block(result);
}];
}
+ (void)NetWorkWithURL:(NSString *)strURL Body:(NSString *)bodyStr block:(Block)block{
NSString *strEnCode = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strEnCode];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:bodyData];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
block(result);
}];
}