Nice to meet you.
I would like to exchange JSON files during the synchronous communication process in Objective-C. I don't know exactly which method is good.
I would like to know how to not use AFNetworking
.
There are many ways to search on Google.
Professor, thank you for your cooperation.
ios objective-c json
There are a few, so just how to choose.
[NSDataDataWithContentsOfURL:url]
blocks processing when runs on the main thread.
DataWithContentsOfURL Reference contains
Do not use this synchronous method to request network-based URLs.For network-based URLs, this method can block the current thread for seconds on a slow network, result in a previous user experience, and in iOS, may cause your app to be terminated.
should not be used to retrieve data on the network.
Use NSURLDownload to write files directly locally. Geekly Page: NSURLDownload is easy to understand.
NSURLSession is a class that encapsulates the tedious implementations required by NSURLConnection.You should use this for iOS7 and later. NSURLSession summary is helpful.
NSURLConnection is available if you want to implement it in detail, but I think NSURLSession is basically enough.
If possible, read the Apple documentation URL Loading System Programming Guide.It's very detailed when and what to choose.
I think it's like this.
NSURLRequest* urlRequest=[NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://www.physics.leidenuniv.nl/json/news.php"];
NSURLResponse* response=nil;
NSError* error=nil;
NSData* data = [NSURLConnection sendSynchronousRequest: urlRequest returningResponse: & response error: & error ];
NSMutableData*responseData=[NSMutableData alloc] init];
responseData appendData:data;
NSSstring* responseString=[NSSstring alloc] initWithData:responseData encoding:NSUTF8 StringEncoding];
NSError*e=nil;
NSData*jsonData= [responseString dataUsingEncoding: NSUTF8 StringEncoding];
NSDictionary* JSON = [NSJSONSERIALIZATION JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error: & e];
The simple one is NSData#dataWithContentsOfURL:
.NSURL*url=[NSURLURLWithString:@"http://example.com"];
NSData*data=[NSDataDataWithContentsOfURL:url];
Decoding JSON will be done separately using NSJSONSERIALIZATION.
© 2024 OneMinuteCode. All rights reserved.