1,Block封装post异步网路请求
2,AFNetworking第三方封装
// ViewController.m
// 6.23.UI
//
// Created by rimi on 15/6/23.
// Copyright (c) 2015年 rectinajh. All rights reserved.
//
#import "ViewController.h"
#import "NetWorkingManager.h"
#define APP_KEY @"79396f95d9bf1e813672358f2890cfa7"
@interface ViewController ()<NetWorkingManagerDelegate>
- (IBAction)buttonPressed:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)requestFinished:(id)successObject{
NSLog(@"ViewController类中实现的协议方法:%@",successObject);
}
- (void)requestFail:(id)errorDescription{
}
- (IBAction)buttonPressed:(id)sender {
// //配置body
//key = 您申请的KEY&pageindex = 1 & pagesize = 2 &keyword =上海
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:@"1" forKey:@"pageindex"];
[dic setObject:@"1" forKey:@"pagesize"];
[dic setObject:@"上海" forKey:@"keyword"];
[dic setObject:@"79396f95d9bf1e813672358f2890cfa7" forKey:@"key"];
NetWorkingManager *manager = [[NetWorkingManager alloc]init];
[manager sendAsynWithPOST:@"http://api2.juheapi.com/xiecheng/senicspot/ticket/search" param:dic success:^(id responseObject) {
NSLog(@"block中的传值数据:%@",responseObject);
//刷新界面操作
}];
}
@end
// NetWorkingManager.h
// 6.23.UI
//
// Created by rimi on 15/6/23.
// Copyright (c) 2015年 rectinajh. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef void (^Block)(id responseObject);
@protocol NetWorkingManagerDelegate<NSObject>
- (void)requestFinished:(id)successObject;
- (void)requestFail:(id)errorDescription;
@end
@interface NetWorkingManager : NSObject
//为什么delegate对象用weak?(手动管理下,避免循环引用)为什么用id?(签订协议的人不同)assign与weak?(arc模式下用weak)
@property (nonatomic ,weak)
// http://www.z081.com/first.html 比较好的解释
id<NetWorkingManagerDelegate> delegate;
- (void)sendAsynWithPOST:(NSString *)urlString
param:(NSMutableDictionary *)dictionary;
//封装成BLOCK
- (void)sendAsynWithPOST:(NSString *)urlString
param:(NSMutableDictionary *)dictionary
success:(Block)successBlock;
@end
封装部分
// NetWorkingManager.m
// 6.23.UI
//
// Created by rimi on 15/6/23.
// Copyright (c) 2015年 rectinajh. All rights reserved.
//
#import "NetWorkingManager.h"
#import "Manager.h"
@interface NetWorkingManager ()<NSURLConnectionDelegate>
{
NSMutableData * _data;
}
//
@property(nonatomic, copy)Block success;
@end
@implementation NetWorkingManager
- (void)sendAsynWithPOST:(NSString *)urlString
param:(NSMutableDictionary *)dictionary
{
NSMutableString *bodystring = [NSMutableString string];
for (NSString *key in dictionary) {
//key = dic[key]
[bodystring appendFormat:@"%@=%@&",key ,dictionary[key]];
}
NSData *data = [bodystring dataUsingEncoding:NSUTF8StringEncoding];
//创建对象
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
//确定request
urlRequest.HTTPMethod = @"POST";
[urlRequest setHTTPBody:data];
//发送请求
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
#pragma mark - block调用
Manager *manager = [[Manager alloc]init];
[manager sendNetworkwithURL:@"" block:^(id object) {
NSLog(@"%@",object);
}];
}
#pragma mark - 封装成BLOCK
- (void)sendAsynWithPOST:(NSString *)urlString
param:(NSDictionary *)dictionary
success:(Block)successBlock
{
NSMutableString *string = [NSMutableString string];
for (NSString *key in dictionary) {
//key = dic[key]
[string appendFormat:@"%@=%@&",key ,dictionary[key]];//字符串拼接不能空格
}
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
//创建对象
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
//确定request
urlRequest.timeoutInterval = 10;
urlRequest.HTTPMethod = @"POST";
[urlRequest setHTTPBody:data];
//发送请求
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
//记录block
self.success = successBlock;
}
#pragma mark -- 四个协议 正式协议protocol(类名加delegate),非正式协议,创建多个协议只是占用代码区的内存
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%@", error.localizedDescription);
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"收到请求,开始回复");
_data = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//NSLog(@"%@", _data);
NSError * error = nil;
id object = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"%@", error.localizedDescription);
}
//NSLog(@"%@", object);
//使用代理传值
//判断delegate对象存在,方法实现
// if (_delegate && [_delegate respondsToSelector:@selector(requestFinished:)]) {
//
// [_delegate requestFinished:object];
// }
//使用block传值
self.success(object);
}
@end
AFNetworking第三方封装
https://github.com/AFNetworking/AFNetworking
将第三方库,添加到工程
#import "AFNetworking.h"
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:@"1" forKey:@"pageindex"];
[dic setObject:@"1" forKey:@"pagesize"];
[dic setObject:@"上海" forKey:@"keyword"];
[dic setObject:@"79396f95d9bf1e813672358f2890cfa7" forKey:@"key"];
#pragma mark - AfnetWorking第三方网络请求
AFHTTPRequestOperationManager *maneger = [AFHTTPRequestOperationManager manager];
[maneger POST:@"http://api2.juheapi.com/xiecheng/senicspot/ticket/search" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error reason:%@",error.localizedDescription);
}];