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.