• 封装的网路请求类


      

    + (ASIHTTPRequest *)requestWithURL:(NSString *)urlstring

                                params:(NSMutableDictionary *)params

                            httpMethod:(NSString *)httpMethod

                                 block:(FinishLoadHandle)block {

        NSString *url= [NSStringstringWithFormat:@"%@%@?",adrNAME,urlstring];

        //判断是否为GET请求

        NSComparisonResult comparRet1 = [httpMethod caseInsensitiveCompare:@"get"];

        if (comparRet1 == NSOrderedSame) {

            

            //------如果是GET请求,则将请求参数拼接在URL后面---------------

            NSArray *allkeys = [params allKeys];

            //&username=12345@qq.com&password=123456

            NSMutableString *paramsString = [NSMutableStringstring];

            for (int i=0; i<allkeys.count; i++) {

                NSString *key = [allkeys objectAtIndex:i];

                NSString *value = [params objectForKey:key];

                if (i == allkeys.count-1) {

                    [paramsString appendFormat:@"%@=%@",key,value];

                }else{

                    [paramsString appendFormat:@"%@=%@&",key,value];

                }

            }

            

            if (paramsString.length > 0) {

                url = [url stringByAppendingString:paramsString];

            }

        }

        

        

    //NSLog(@"请求的地址>>%@",url);

        

        __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];

        [ShareAppshowLoading];

        return request;

    }

  • 相关阅读:
    MySQL之触发器
    MySQL之视图
    MySQL之多表查询(笛卡尔积查询、内连接、外连接(左外连接,右外连接)、union、union all )
    MySQL之子查询
    MySQL之关键字
    mysql之内连接,外连接(左连接,右连接),union,union all的区别
    mysql之魔鬼训练营
    MySQL之innodb和myisam的区别
    springmvc+spring+mybatis 项目配置
    Css3 伪元素
  • 原文地址:https://www.cnblogs.com/jaj-angel/p/3653640.html
Copyright © 2020-2023  润新知