IPHONE JSON 另一个例子
来源:互联网 发布:淘宝如何做数据包 编辑:程序博客网 时间:2024/04/28 17:33
// Send an asyncronous request on the queue [NSURLConnectionsendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse*response, NSData *data, NSError *error) { // If there was an error getting the data if (error) { dispatch_async(dispatch_get_main_queue(), ^(void) { completionBlock(nil, error); }); return; } // Decode the data NSError *jsonError; NSDictionary*responseDict = [NSJSONSerialization JSONObjectWithData:data options:0error:&jsonError]; // If there was an error decoding the JSON if (jsonError) { dispatch_async(dispatch_get_main_queue(),^(void) { }); return; } // All looks fine, lets call the completion block with the response data dispatch_async(dispatch_get_main_queue(), ^(void) { completionBlock(responseDict, nil); }); }]; }
RAW
So, I am working on a little app that front an API back-end I am also writing and have been watching the WWDC 2011 videos on GCD and Blocks... thought I would give all this a go!
It's pretty simple, heres an example request:
[SHAPIController performRequestWithUri:@"/your/api/method"params:nil completionHandler:^(NSDictionary *response,NSError *error) { if (error) { NSLog(@"%@", error); } NSLog(@"%@", response); }];
RAW
The second NSLog
will output something like this (of course depending on the API):
{ some = json; yeah = ( yeah, sdfsdf, sdfdsf ); }
RAW
The completion block will always be called on the main thread so you can update your UI or whatever there.
Hope its useful to someone :)
- IPHONE JSON 另一个例子
- IPHONE JSON 一个例子
- 另一个例子
- 里是另一个例子
- 递归小例子(另一个)
- json例子
- json例子
- JSON例子
- JSON 例子
- 另一个工厂模式的例子
- 另一个语音识别的例子
- iPhone基础学习例子
- iPhone 例子代码
- iphone项目例子资源
- ios iphone 入门 例子
- iPhone项目例子资源
- iPhone项目例子资源
- iPhone上的JSON
- VS编辑器快捷键技巧
- 【编程语言】java--List及其子类
- Hibernate中对增删改查的小结
- VI很全很强大
- G题
- IPHONE JSON 另一个例子
- 在互联网时代不突破的企业将没有出路
- HibernateUtil 获取sessionfactory 、session
- 免刻盘,U盘移动硬盘引导安装UBUNTU
- android项目源码
- GDI+ 小知识点总结
- Socket IO模型之IOCP
- 分享几个下载免费电子图书的地方ebook
- 详解Android首选项框架ListPreference