网络发送同步请求——(开发中一般不会使用,但是还是了解的好)

来源:互联网 发布:新浪微博数据抓取 编辑:程序博客网 时间:2024/06/11 20:55

 新建工程代码如下:

////  ViewController.m//  异步连接参数解析////  Created by apple on 15/10/26.//  Copyright (c) 2015年 LiuXun. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        // 1 url    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/demo.json"];        // 2. 建立请求    NSURLRequest *request = [NSURLRequest requestWithURL:url];        // 3. 建立连接Connection,发送同步请求到服务器    /**     参数:     SynchronousRequest:发送同步请求     returningResponse:服务器响应的地址     error:错误信息的地址     */    NSURLResponse *response = nil;    NSData *data =   [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];    NSLog(@"reponse= %@", response);    NSLog(@"%@——%@",  [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding], [NSThread currentThread]);}@end

运行结果如下:


0 0
原创粉丝点击