- (NSDictionary*)requestDataWithMethod:(NSString *)method{
//含中文转换需要转码
NSString *str = [method stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//构建对象
NSURL *url = [NSURL URLWithString:str];
//创建网络请求
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.f];
//创建同步连接
NSURLResponse *response = nil;
NSError *error = nil;
//data存放二进制数据
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
//返回请求到的字典
return dic;
}
从网络获取图片
//取出字典中images对应的value
NSDictionary *tempDic = [[[temp1 valueForKey:@"subjects"] objectAtIndex:0] objectForKey:@"images"];
//从字典中再取出key对应的对象(网址:nsstring)
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[tempDic objectForKey:@"small"]]];
//取出
NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *tempImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 200)];
tempImageView.center = self.view.center;
[self.view addSubview:tempImageView];
//获得图片
tempImageView.image = [UIImage imageWithData:data];