网络请求


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

//    [self loadData1];

     [self loadData5];

}

- (void)loadData1

{

//把字符串 转成 nsurl

    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];

    NSString *content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

    NSLog(@"%@",content);

}

- (void)loadData2

{

    NSURL *url = [NSURL URLWithString:@"http://preview.quanjing.com/is_rm001/is0997q92.jpg"];

    NSData *data = [NSData dataWithContentsOfURL:url];

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];

    imageView.contentMode = UIViewContentModeScaleToFill;

    imageView.image = [UIImage imageWithData:data];

    [self.view addSubview:imageView];

}

//网络请求 :同步请求 异步请求

//同步请求 :等所有操作完成执行完毕  才会继续执行

//同步请求的弊端  会遇到假死的状态 (只要请求的操作  没有执行完毕就不会再去响应 任何事件(在同一线程))


//异步请求:在程序远行的时候  会利用空闲的时间去执行里面的操作  不会影响到同一线程里面的其他操作


//同步请求

- (void)loadData3

{

    NSURL *url = [NSURL URLWithString:@"http://img6.3lian.com/c23/desk4/06/126/d/11.jpg"];

    

//    实例化 请求对象 里面携带者请求的地址

    NSURLRequest *request = [NSURLRequest                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   requestWithURL:url];

//    data 服务器响应(返回)给我们的数据

//    NSURLConnection 是发送请求的类

   NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];

    imageView.contentMode = UIViewContentModeScaleToFill;

    imageView.image = [UIImage imageWithData:data];

    [self.view addSubview:imageView];

    

}


 //异步请求

- (void)loadData4

{

    NSURL *url = [NSURL URLWithString:@"http://i441.photobucket.com/albums/qq139/shengzhe1/02mg/m150/zy44.jpg"];

    

    //    实例化 请求对象 里面携带者请求的地址

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    

//    需要通过连接异步发送请求

//    线程 queue (队列)

    NSOperationQueue *queue = [[NSOperationQueue alloc]init];

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];

//    发送一个异步请求 queue 这个线程里面去执行

    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

//     response 服务器回应的内容  (回应状态的code 以及 error

//        data 回应 给客户端需要的数据

        NSLog(@"%@",response);

         imageView.image = [UIImage imageWithData:data];

    }];

    

    imageView.contentMode = UIViewContentModeScaleToFill;

//    imageView.image = [UIImage imageWithData:data];

    [self.view addSubview:imageView];

}

//get 把传输的数据 放在连接地址里面

- (void)loadData5

{

NSString *interfaceString = @"http://apis.baidu.com/showapi_open_bus/mobile/find";

    NSString *requestContentSreing = @"num=18785044865";

    NSString *urlString = [NSString stringWithFormat:@"%@?%@",interfaceString,requestContentSreing];

//    把连接地址字符串  转成NSUTF8StringEncoding

    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

//    可变请求可以添加请求方式  以及请求的请求头 或者更多

// NSTimeInterval 请求所需的时间  超过时间 不再发送请求

//    url cachePolicy  缓存内容的方式

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:10];

//    指定http的请求方式

    request.HTTPMethod = @"GET";

    NSString *apiKey = @"e7f5ac9e7c42a6c8cb125ee1d7e8779e";

//    apiKey 发送给服务器指定的请求头的位置

//    forHTTPHeaderField 需要的key 是服务器指定的key

    [request addValue:apiKey forHTTPHeaderField:@"apikey"];

    

    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        NSLog(@"%@",response);

//        NSLog(@"%@",data);

//        解析json文件

//        data 转换成json 文件

        NSError *error;

       NSDictionary *info = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

        

        NSLog(@"%@",info);

        NSLog(@"%@",info[@"showapi_res_body"][@"prov"]);

        

    }];

    

}

//post 请求

- (void)loadData6

{

    NSURL *url = [NSURL URLWithString:@"http://www.weihuok.com/customer2/GetService"];

//    PlatformType是设备类型  3 iOS

//    请求的参数

    NSDictionary *dictionary = @{@"PlatformType":@"3"};

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];

//    设置HTTP请求的方式

    request.HTTPMethod = @"POST";

//    设置请求的参数

//    dataUsingEncoding把字符串转换成nsdata 类型

//    HTTPBody要的是data

    request.HTTPBody = [[NSString stringWithFormat:@"%@",dictionary]dataUsingEncoding:NSUTF8StringEncoding];

     NSOperationQueue *queue = [[NSOperationQueue alloc]init];

    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

      NSDictionary *info = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

        

        NSLog(@"======%@",info);

    }];

    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值