废话不多说 上代码
》》》》》》》》》》》》》》》》》头文件:
//
// RequestInterface.h
// ThankYou
//
// Created by ming on 13-9-27.
// Copyright (c) 2013年 ming. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
typedef void (^Success)(id object);
typedef void (^Fail) (id error);
@interface RequestInterface : NSObject<ASIHTTPRequestDelegate>
/*
void (^)(id)
void:返回类型
^:block标记
id:参数类型
requstDic:请求对象包括:get/post ,方法名,参数
success:成功
fail:失败
*/
-(void)shareRequestIntface:(NSDictionary *)requestDic
RequstSuccess:(Success)success
RequstFail:(Fail)fail;
+(RequestInterface *)shareInstance;
@end
》》》》》》》》》》》》》》》》实现m文件:
//
// RequestInterface.m
// ThankYou
//
// Created by ming on 13-9-27.
// Copyright (c) 2013年 ming. All rights reserved.
//
#import "RequestInterface.h"
#import "SBJson.h"
@implementation RequestInterface
static __weak Success successBlock;
static __weak Fail failBlock;
-(id)init
{
self=[super init];
if ( self==nil) {
}
return self;
}
//单例
+(RequestInterface *)shareInstance
{
static RequestInterface *shareInterface=nil;
if (shareInterface==nil) {
shareInterface=[[RequestInterface alloc]init];
}
return shareInterface;
}
#pragma mark -----PublicRequest
//调用接口
-(void)shareRequestIntface:(NSDictionary *)requestDic
RequstSuccess:(Success)success
RequstFail:(Fail)fail
{
// 登陆接口例子
/*@{
@"methodType":@"GET",//请求方式post或get,必填
@"methodName":@"login",//login从接口文档中取出/***.php中的***,必填
@"username":@"ming",//根据接口字段而定
@"password":@"1234"//根据接口字段而定
};*/
//block方法
successBlock=[success copy];
failBlock=[fail copy];
//请求方式和请求名称
NSString *requestType=[requestDic objectForKey:@"methodType"];
NSString *requestName=[requestDic objectForKey:@"methodName"];
NSString* p = @"";
//post提交数据
NSMutableDictionary *postDic=[NSMutableDictionary dictionary];
//获取参数
for (NSString* key in requestDic) {
if (![key isEqualToString:@"methodType"]&&![key isEqualToString:@"methodName"]) {
NSObject* value = [requestDic objectForKey:key];
p = [p stringByAppendingFormat:@"%@=%@&",key,value];//get请求获取参数
[postDic setObject:value forKey:key];//从传入的参数中抽取需要post的数据
}
}
p=[p stringByAppendingFormat:@"channal=%@&pw=%@",@"appshow",@"1c6302ed3216927cfac2b77b66c2a77d"];
p = [p stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[postDic setObject:@"appshow" forKey:@"channal"];
[postDic setObject:@"1c6302ed3216927cfac2b77b66c2a77d" forKey:@"pw"];
NSString *urlstr=[NSString stringWithFormat:@"%@%@.php",URL,requestName];
//post请求地址
NSURL *method_post_url=[NSURL URLWithString:urlstr];
//get请求地址
NSURL *method_get_url = [NSURL URLWithString:COMBINE3(urlstr,@"?", p)];
if ([requestType isEqualToString:@"GET"]) {
ASIHTTPRequest * g_request = [[ASIHTTPRequest alloc]initWithURL:method_get_url];
[g_request setUserInfo:@{@"method": requestName}];
[g_request setRequestMethod:requestType];
[g_request setDelegate:self];
[g_request startAsynchronous];
}
else{
ASIFormDataRequest *p_request=[ASIFormDataRequest requestWithURL:method_post_url];
for (NSString *key in postDic) {
NSString *value=[postDic objectForKey:key];
[p_request setPostValue:value forKey:key];
}
[p_request setUserInfo:@{@"method": requestName}];
[p_request setRequestMethod:requestType];
[p_request setDelegate:self];
[p_request startAsynchronous];
}
}
#pragma mark -----ASIRequstDelegate
-(void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
SBJsonParser * parser = [[SBJsonParser alloc] init];
NSError * error = nil;
NSMutableDictionary *jsonDic = [parser objectWithString:responseString error:&error];
successBlock(jsonDic);//block回调
}
-(void)requestFailed:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
SBJsonParser * parser = [[SBJsonParser alloc] init];
NSError * error = nil;
NSMutableDictionary *jsonDic = [parser objectWithString:responseString error:&error];
failBlock(jsonDic);//block回调
}
@end