使用asi请求的步骤

    __blockASIFormDataRequest *request = [ASIFormDataRequestrequestWithURL:[NSURLURLWithString:url]];

    NSLog(@"%@",url);

    //设置请求方式

    [request setRequestMethod:httpMethod];

    //设置超时时间

    [request setTimeOutSeconds:30];

    //判断是否为POST请求

    NSComparisonResult comparRet2 = [httpMethod caseInsensitiveCompare:@"POST"];

    if (comparRet2 == NSOrderedSame) {

        for (NSString *key in params) {

            id value = [params objectForKey:key];

            //判断是否是上传文件

            if ([value isKindOfClass:[NSData class]]) {

                [request addData:value forKey:key];

            } else {

                [request addPostValue:value forKey:key];

            }

        }

    }

        

    //请求成功之后调用的block

    

    //requestCompletionBlock 产生了循环引用

    //request---->block

    //block------>request

    [request setCompletionBlock:^{

        [ShareApp dissLoading];

        NSString *responsestring = [[request.responseString  stringByReplacingOccurrencesOfString:@""withString:@""]stringByReplacingOccurrencesOfString:@""withString:@""];

       // NSLog(@"%@ %@",responsestring,request.responseString);

        //NSLog(@"%@",[[request.responseString  stringByReplacingOccurrencesOfString:@"" withString:@""]stringByReplacingOccurrencesOfString:@"" withString:@""]);

        //[request.responseString ]

//        NSError *error;

//        NSArray *aaaa = [NSJSONSerialization JSONObjectWithData:[responsestring dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];

        id results;

        if (!responsestring) {

            UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:nilmessage:@"网络异常" delegate:nilcancelButtonTitle:@"OK"otherButtonTitles: nil];

            [alertView show];

            [alertView release];

 

            return ;

        }

        NSData *responseData = [responsestring dataUsingEncoding:NSUTF8StringEncoding];

        //        UIDevice *device = [UIDevice currentDevice];

        //        float version = [device.systemVersion floatValue];

        float version = WXHLOSVersion();

 

 

        

        

        if (version >= 5.0) {

            results = [NSJSONSerializationJSONObjectWithData:responseData options:NSJSONReadingMutableContainerserror:nil];

        }

        if ([responsestring isEqualToString:@"用户名或密码错误"]) {

            results = responsestring;

        }

        if (!results) {

            results = responsestring;

        }

    

        //回调block

        if (block != nil) {

            block(results);

        }

    }];

    

    //请求失败之后调用的block

    [request setFailedBlock:^{

        [ShareApp dissLoading];

        block(nil);

    }];

    

    //开始异步请求

    [request startAsynchronous];

posted @ 2014-04-09 10:47  jian啊jian  阅读(372)  评论(0编辑  收藏  举报