Download data synchronously, parse the data in the jeson file, and parse the data in the jeson file.

Source: Internet
Author: User

Download data synchronously, parse the data in the jeson file, and parse the data in the jeson file.

# Import "ViewController. h "@ interface ViewController () @ property (nonatomic, weak) IBOutlet UIButton * button; @ property (nonatomic, weak) IBOutlet UIImageView * imageView; @ end @ implementation ViewController-(IBAction) btnClick :( id) sender {// give a url string NSString * str = @ "http://iappfree.candou.com: 8080/free/applications/limited? Currency = RMB & page = 3 "; NSString * str2 = @" http://photo.candou.com/ I /114/826ea823e8ffe792a6fda9e126f6c404 "; // encapsulate it into available URL types; NSURL * url = [NSURL URLWithString: str]; NSURL * url2 = [NSURL URLWithString: str2]; [self syncDownloadWithString: url]; [self syncDownloadWithData: url2] ;}# synchronous request of The pragma mark-NSDate Method -- (void) syncDownloadWithData :( NSURL *) url {// synchronous request method provided by binary data NSData * data = [NSData dataWithContentsOfURL: url]; // NSData --> UIImage * image = [UIImage imageWithData: data]; // UIImage --> NSData * dataImage = UIImagePNGRepresentation (image); NSData * dataImage2 = UIImageJPEGRepresentation (image, 0.5); UIImage * image3 = [UIImage imageWithContentsOfFile: @ ""]; self. imageView. image = image; NSLog (@ "downloaded") ;}# synchronous request of The pragma mark-NSString Method -- (void) syncDownloadWithString :( NSURL *) url {// the synchronous Request Method NSError * error provided by the string; NSString * str = [[NSString alloc] initWithContentsOfURL: url encoding: NSUTF8StringEncoding error: & error]; if (error) {NSLog (@ "% @", error); return ;}// NSString ---> NSData; NSData * data = [str dataUsingEncoding: NSUTF8StringEncoding]; // NSDate --> 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 didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.