码迷,mamicode.com
首页 > 其他好文 > 详细

同步下载数据,jeson 解析

时间:2015-08-29 14:03:17      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:数据   下载   同步下载   网络   解析   

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,weak)IBOutlet UIButton *button;

@property(nonatomic,weak)IBOutlet UIImageView *imageView;


@end


@implementation ViewController

-(IBAction)btnClick:(id)sender
{
    //给一个url字符串
    NSString *str = @"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=3";
    
    NSString *str2 = @"http://photo.candou.com/i/114/826ea823e8ffe792a6fda9e126f6c404";
    //封装成可用的URL类型;
    NSURL *url = [NSURL URLWithString:str];
    NSURL *url2 = [NSURL URLWithString:str2];
    
    [self syncDownloadWithString:url];
    [self syncDownloadWithData:url2];
}

#pragma mark -NSDate类方法的同步请求-

-(void)syncDownloadWithData:(NSURL *)url{
    //二进制数据提供的同步请求方法
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    //NSData -->UIImage
    UIImage *image = [UIImage imageWithData:data];
    
    //UIImage -->NSData
    NSData *dataImage = UIImagePNGRepresentation(image);
    NSData *dataImage2 = UIImageJPEGRepresentation(image, 0.5);
    UIImage *image3 = [UIImage imageWithContentsOfFile:@""];
    
    
    
    
    
    self.imageView.image = image;
    
    NSLog(@"下载完毕");
}

#pragma mark -NSString类方法的同步请求-
-(void)syncDownloadWithString:(NSURL *)url{

    //字符串提供的同步请求的方法
    NSError *error;
    NSString *str = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
    if (error) {
        NSLog(@"%@",error);
        return;
    }
    //NSString --->NSData;
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    
    //NSDate -->NSString
    NSString *strData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",strData);
    
    
    
    
    NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"%@",dict);
    
    
    NSLog(@"str:%@",str);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

版权声明:本文为博主原创文章,未经博主允许不得转载。

同步下载数据,jeson 解析

标签:数据   下载   同步下载   网络   解析   

原文地址:http://blog.csdn.net/u012701023/article/details/48086723

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!