今天开始写博客啦,一开始肯定是不会很完美,希望我能坚持住,同时也希望在写博客的过程中能使自己的技术有所提高。也希望能够结识更多地码农们,我们一起进步,上帝加力量,下面就开始拉哈。
1 get请求
gei请求是网络接口和参数都使用字符串拼接到一起的网络请求方式,示例如下
// 1 创建请求
NSString *string = @"http://images.nike.com/is/image/DotCom/THN_PS/Nike-VRS-Covert-20-Driver-Right-Handed-Golf-Club-GD1487_001_A.jpg?fmt=jpg&qty=85&wid=300&hei=300&bgc=F5F5F5";
string = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:string];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
request.HTTPMethod = @"GET";
// 2 建立链接 链接服务器
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// 3 处理数据
NSError *jsonError = nil;
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
if (jsonError) {
NSLog(@"错误信息%@",jsonError);
}
self.imageView.image = [UIImage imageWithData:data];
2 post请求