用bolck对ASIHttpRequst进行简单的封装

废话不多说 上代码

》》》》》》》》》》》》》》》》》头文件:

//
//  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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值