• NSURLConnection和NSMutableURLRequest 实现同步、异步请求


    我是走向ios的一个小书童,我有很多不懂的,新鲜的知识去学习,去掌握!

    我首先要吐槽一下:
    那些不负责的博友!你分享知识本来是好事!可是你直接Control+V就是你的不对了!
    尼玛,直接Control+V个错误的代码,这真是“雪中送屎”啊!坑爹啊!
    真心的希望,大家分享的代码能够质量高一些,再高一些!
     
    废话不多说了。最近,工作中用到了NSURLConnection进行请求网络数据。
    只有先把自己搞出来的,分享给大家!也希望大家能给一些建议!
     

    一、同步请求-GET方式

     

    // 要请求的地址

        NSString *urlString=@"地址,我就只有保密了!你懂的";

    // 将地址编码

        urlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

        // 实例化NSMutableURLRequest,并进行参数配置

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    [request setURL:[NSURL URLWithString: urlString]];

    [request setCachePolicy:NSURLRequestReloadIgnoringCacheData];

    [request setTimeoutInterval: 60];

    [request setHTTPShouldHandleCookies:FALSE];

    [request setHTTPMethod:@"GET"];

     

        // Response对象,用来得到返回后的数据,比如,用statusCode==200 来判断返回正常

        NSHTTPURLResponse *response;

        NSData *returnData = [NSURLConnection sendSynchronousRequest:request

                                                   returningResponse:&response error:nil];

     

        // 处理返回的数据

        NSString *strReturn = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

        NSLog(@"%@",strReturn);

        NSLog(@"%d",[response statusCode]);

        // 对象还是要释放的

        [request release];

        [strReturn release];

     
    关于,同步GET请求,也没有什么好说的。我都做了备注!
     
    二、异步请求-GET方式
     
    这种,方式请求就有点麻烦了!因为是异步的嘛。
    根据以下步骤:
    1.在*.h文件中,实现NSURLConnectionDelegate协议。
    例如:

    @interface MoreViewController : UIViewController<</span>NSURLConnectionDelegate>

     
    2.在*.m文件中,进行异步请求和实现协议方法。
     
    异步请求:
     

    NSString *urlString=@"地址,我就只有保密了!你懂的";

    urlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    [request setURL:[NSURL URLWithString: urlString]];

    [request setCachePolicy:NSURLRequestReloadIgnoringCacheData];

    [request setTimeoutInterval: 60];

    [request setHTTPShouldHandleCookies:FALSE];

    [request setHTTPMethod:@"GET"];

        // NSURLConnection* aSynConnection 可以申明为全局变量.

        // 在协议方法中,通过判断aSynConnection,来区分,是哪一个异步请求的返回数据。

        aSynConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

     
    协议方法:
     

     

    #pragma mark- NSURLConnectionDelegate 协议方法

     

     

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse{

        NSLog(@"请求成功!");

        returnInfoData=[[NSMutableData alloc]init];

    }

     

     

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

        [returnInfoData appendData:data];

    }

     

     

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

        NSLog(@"didFailWithError");

    }

     

     

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection{

        if( [connection isEqual: aSynConnection])

        {

            NSString *asyReturn = [[NSString alloc] initWithData:returnInfoData encoding:NSUTF8StringEncoding];

            NSLog(@"%@",asyReturn);

            [returnInfoData release];

            returnInfoData = nil;

            [asyReturn release];

        }

    }

     
    关于,POST请求,因为没有地址让我测试,就暂时,先不贴了!
    就是要设置一些POST的相关参数:

        [request setHTTPMethod:@"POST"];

        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

        [request setHTTPBody:postData];

     
    等有机会,让我测试通了,在贴!
     
    但是,我发现,这样的事情:
    上边提及的几个协议方法,是过期了的。所以,虽然能用,但是,总感觉不好。应该有替代的相关方法吧。
     
    于是,继续追寻。终于找到了!
     
    原来,苹果官方,将NSURLConnectionDelegate协议废除(虽然还能用),并使用NSURLConnectionDataDelegate协议来代替。并且重写了相关的一些方法。分别看一下,这两个协议就清楚了。
     

    @protocol NSURLConnectionDelegate <<span style="color: #743fa4">NSObject>

    @protocol NSURLConnectionDataDelegate <<span style="color: #743fa4">NSURLConnectionDelegate>

     
    做个标记,以待来着!
     
    希望对您有所帮助!
  • 相关阅读:
    常用数据库种类 以及优缺点
    数据库
    Python中os与sys两模块的区别
    python操作数据库
    python——解释型语言
    面向对象的三大特性
    Android Fastboot 与 Recovery 和刷机 千山万水迷了鹿
    selenium与appium怎样联系
    Pycharm快捷键
    uiautomator python版本
  • 原文地址:https://www.cnblogs.com/lisa090818/p/3435239.html
Copyright © 2020-2023  润新知