• 使用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];

  • 相关阅读:
    Spring → 01:概述
    获取html中所有img
    设置app的语言
    iOS UITableview 图片懒加载demo
    获取UIWbview中的所有图片
    objective C socket 库
    UITableView 索引的设置
    objective c md5 sha1 sha256 AES加密库
    UITableviewCell 横滑出现多个按钮的三方库
    iOS微信朋友圈 评论点击姓名功能 (补充)
  • 原文地址:https://www.cnblogs.com/jaj-angel/p/3653671.html
Copyright © 2020-2023  润新知