网络解析

网络解析 又叫网络编程、网络请求
完整的网络请求分为三步:
1>url NSURL 请求网络地址
2>request 请求对象
3>connection 连接对象
其中2>又分为两种:get请求和post请求
3>也分为两种:同步连接和异步连接
另外 同步连接分为两种get和post,分别对应2>中的get和post
异步连接也分为两种:delegate 和 block
delegate的设置又分为两种 alloc init 方法 和 便利构造器方法
注意:如果网络地址格式为:http://xxx?strDate=2015-08-08&strRow=1&strMS=1 则2>中get请求和post请求都可用,
如果网络地址格式为:http://xxx.php 则2>中只能用get


具体方法如下:
方法一、get请求 同步连接
1>请求网络地址
NSURL *url = [NSURL URLWithString:@“http://xxxx /getC_N?strDate=2015-08-09&strRow=1&strMS=1”];
2>请求对象
NSURLRequest *request = [[NSURLRequest alloc] initWithURL : url cachePolicy : 0 timeoutInterval : 10];//里面填的三项分别为:url 缓冲策略 最长请求时间
3>建立连接
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:get_request returningResponse:&response error:&error];
//解析网络数据
NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSDictionary *contentEntityDic = [rootDic objectForKey:@“contentEntity”];
NSString *sGW = contentEntityDic[@“sGW”];
NSLog(@"%@",sGW);


方法二、post请求 同步连接
1>请求网络地址
NSURL *url2 = [NSURL URLWithString:@“http://xxxx”];
2>请求对象
NSMutableURLRequest *post_request = [[NSMutableURLRequest alloc]initWithURL:url2 cachePolicy:0 timeoutInterval:10];
[post_request setHTTPMethod:@“POST”];
[post_request setHTTPBody:[@“strDate=2015-08-09&strRow=1&strMS=1” dataUsingEncoding:NSUTF8StringEncoding]];
3>建立连接 post方法(保密性较高)
NSData *data2 = [NSURLConnection sendSynchronousRequest : request returningResponse:nil error:nil];
//解析网络数据
NSDictionary *rootDic2 = [NSJSONSerialization JSONObjectWithData:data2 options:0 error:nil];
NSDictionary *contentDic = [rootDic2 objectForKey:@“contentEntity”];
NSString *sGW2 = contentDic[@“sGW”];
NSLog(@"%@",sGW2);


方法三、get 请求 异步连接block方法
1>、2>同 get请求 同步连接get方法
3>
[NSURLConnection sendAsynchronousRequest : request queue : [NSOperationQueue mainQueue] completionHandler : ^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%@",response);
//解析网络数据data(放在内部解析)
NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];//注意 data是请求完成之后的data
NSDictionary contentEntityDic = [rootDic objectForKey:@“contentEntity”];
NSString sGW = contentEntityDic[@“sGW”];
NSLog(@"%@",sGW);
}];
方法四、get 请求 异步连接delegate方法
1>、2>同 get请求 同步连接get方法
3>
方法1.alloc init设置 delegate
[[[NSURLConnection alloc]initWithRequest:get_request delegate:self]autorelease];
方法2.便利构器设置 设置delegate
[NSURLConnection connectionWithRequest:get_request delegate:self];
/

第二种异步链接请求方法 :
代理的优点:追踪请求过程
给NSURLConnection对象设置代理 有两种那个方法,一般使用第二种方法
*/
detegate 的解析网络数据放在 viewDidLoad外面

  • (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
    //1.
    NSLog(@"%s%d%@",FUNCTION,LINE,@“收到网络回应”);
    //2.初始化可变数据容器
    self.receiveData = [NSMutableData data];
    }
    // 接收数据的时候出发,参数data为本次接收到的数据
  • (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
    //拼接data
    [self.receiveData appendData:data];
    NSLog(@"%s%d%@",FUNCTION,LINE,@“正在打印数据”);
    }
    //数据接收完成之后执行下面的方法
  • (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
    //此时的data为 接收完成之后的data,可以解析了
    NSLog(@"%s%d%@",FUNCTION,LINE,@“数据传输完成”);
    NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:self.receiveData options:0 error:nil];
    NSDictionary *contentDic = [rootDic objectForKey:@“contentEntity”];
    NSString *sGW = contentDic[@“sGW”];
    NSLog(@"%@",sGW);
    }

猜你喜欢

转载自blog.csdn.net/weixin_39487291/article/details/88575346