网络请求
来源:互联网 发布:php sleep 函数执行 编辑:程序博客网 时间:2024/05/18 17:24
#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);
}];
}
- 网络请求
- 网络请求
- 网络请求
- 网络请求
- 网络请求
- 网络请求
- 网络请求
- 网络请求
- 网络请求
- 网络请求
- 网络请求
- 网络请求
- 网络请求
- 网络请求
- 网络请求
- 网络请求
- 网络请求
- 网络请求
- htmlparser使用指南
- MyBatis入门03 解决字段名与实体类属性名不相同的冲突
- Android菜鸟的成长笔记——Spinner 省市县三级地址选择
- java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredCommentImpl cannot be c
- cocos2dx 常见的49中动作详解
- 网络请求
- 感悟(6)
- 网上Post与Get请求数据步骤
- 从HTML中攫取你所需的信息
- Spring下classpath与classpath*的区别
- 通过pppoe登录校园网
- 扩展 HTMLParser 对自定义标签的处理能力
- 欢迎使用CSDN-markdown编辑器
- 【最大流+二分】bzoj1189 紧急疏散