NSURLConnection获取数据
2024-10-10 05:04:36
- (void)loadDataFromUrl
{
NSURL* url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101190408.html"];
NSMutableURLRequest * urlRequest=[NSMutableURLRequest requestWithURL:url];
NSURLConnection* urlConn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
[urlConn start];
} - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response
{
NSHTTPURLResponse* rsp = (NSHTTPURLResponse*)response;
int code = [rsp statusCode];
if (code != )
{
[connection cancel];
[connection release];
connection = nil;
}
else
{
if (mData != nil)
{
[mData release];
mData = nil;
}
mData = [[NSMutableData alloc] init];
}
} - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[mData appendData:data];
} - (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// [self hideAlert];
NSString* backString = [[NSMutableString alloc] initWithData:mData encoding:NSUTF8StringEncoding];
NSMutableDictionary *backData =[backString JSONValue];
NSLog(@"%@",backData);
connection = nil;
} -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
connection = nil;
} -(void)showAlertView:(NSString*)titleStr
{
UIAlertView *myalert = [[UIAlertView alloc]
initWithTitle:@"提示"
message:titleStr
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:nil];
[myalert show];
[myalert release];
}
最新文章
- svn小设置
- Configuring Network in CentOS 6.3 Virtual Box + Screenshots
- 基于 ThinkPHP 3.2.3 的页面静态化功能的实现
- saltstack之(六)配置管理state
- PAT 05-树8 Huffman Codes
- Ubuntu下eclipse的Extjs提示插件安装
- UMeng 友盟的用户数,启动数 等
- nodejs服务端开发学习笔记
- 某IT校招笔试
- 如何实现自己的ClassLoader
- Spring Boot快速建立HelloWorld项目
- Version 1.6.0 of the JVM is not suitable for the this product.Version:1.8 or greater is required
- HTTP 400 错误 - 请求无效 (Bad request)
- byte[] 解析、转码二三事
- Python高阶函数map、reduce、filter、sorted的应用
- 多线程情况下HashMap死循环的问题
- 6 scrapy框架之分布式操作
- mysql 批量更新的四种方法
- TMG 2010 为HTTPS协议添加非标准端口(443)
- 最近面试js部分试题总结