Ios5 NSURLConnectionLegate未调用方法加载数据
我已经看过了,但似乎没有什么好结果,所以我很好奇为什么我不能得到任何数据 我在Ios5 NSURLConnectionLegate未调用方法加载数据,ios5,asynchronous,Ios5,Asynchronous,我已经看过了,但似乎没有什么好结果,所以我很好奇为什么我不能得到任何数据 我在didReceiveResponse和didReceiveData中设置了断点 它会打印“连接成功”,因此我知道连接已启动 我正在使用ARC进行内存管理 - (void)load { request = [NSMutableURLRequest requestWithURL:myURL
didReceiveResponse
和didReceiveData
中设置了断点
它会打印“连接成功”,因此我知道连接已启动
我正在使用ARC进行内存管理
- (void)load {
request = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
[conn start];
NSLog(@"connection succeeded, %s", [myURL description]);
responseData = [NSMutableData data];
} else {
NSLog(@"connection failed");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
更新:
看看我是如何测试这个的
我确实实现了jonkroll提到的两个方法,在他的回答中,我只是没有显示它们,但是,它们也没有被调用
我之所以添加了
[conn start]
,只是因为它不起作用,我希望它能解决这个问题,但没有这样的运气。NSURLConnection方法initWithRequest
从url开始异步请求数据。因为请求是异步完成的,所以不能期望使用调用请求的相同方法处理响应。相反,您需要在NSURLConnection的委托回调方法中执行此操作。您已经实现了didReceiveResponse:
和didReceiveData:
,但是还有一些对您有用的功能
如果要查看响应的内容,应在connectiondFinishLoading:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// response is complete, do something with the data
NSLog(@"%@", responseData);
}
您的代码打印出“连接成功”这一事实并不意味着请求成功,只意味着成功创建了NSURLConnection对象。要测试连接是否存在问题,可以实现委托方法连接:didFailWithError:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
此外,无需调用[conn start]
。当您调用initWithRequest:
我建议阅读上的苹果文档以了解更多详细信息。NSURLConnection方法
initWithRequest
从url启动异步数据请求。因为请求是异步完成的,所以不能期望使用调用请求的相同方法处理响应。相反,您需要在NSURLConnection的委托回调方法中执行此操作。您已经实现了didReceiveResponse:
和didReceiveData:
,但是还有一些对您有用的功能
如果要查看响应的内容,应在connectiondFinishLoading:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// response is complete, do something with the data
NSLog(@"%@", responseData);
}
您的代码打印出“连接成功”这一事实并不意味着请求成功,只意味着成功创建了NSURLConnection对象。要测试连接是否存在问题,可以实现委托方法连接:didFailWithError:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
此外,无需调用[conn start]
。当您调用initWithRequest:
我建议您阅读上的苹果文档以了解更多详细信息。当您这样声明您的连接时:
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
您正在创建一个本地指针。当方法完成时,由于它是对NSURLConnection
的最后一个强引用,ARC
将其释放。您需要使用强ivar(和/或)属性来保存对所创建的NSURLConnection
的强引用
编辑
下面是我在一个示例项目中测试的基本代码示例。试试看。详细日志记录很有帮助
@implementation <#Your class here#> {
// With ARC ivars are strong by default
NSMutableData *_downloadedData;
NSURLConnection *_connection;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSHTTPURLResponse *realResponse = (NSHTTPURLResponse *)response;
if (realResponse.statusCode == 200){
// Really any 2** but for example
_downloadedData = [[NSMutableData alloc] init];
NSLog(@"Good response");
} else {
NSLog(@"Bad response = %i",realResponse.statusCode);
}
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
if (connection == _connection){
[_downloadedData appendData:data];
NSLog(@"Getting data...");
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
if (connection == _connection){
_connection = nil;
NSLog(@"We're done, inform the UI or the delegates");
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
_connection = nil;
NSLog(@"Oh no! Error:%@",error.localizedDescription);
}
- (void)load {
NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
NSURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
// Assign strong pointer to new connection
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"Connection was initialized? = %@",(!!_connection)?@"YES":@"NO");
}
@end
@实现{
//默认情况下,带弧的IVAR是强的
NSMutableData*_下载数据;
NSURLConnection*_连接;
}
-(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应{
NSHTTPURLResponse*realResponse=(NSHTTPURLResponse*)响应;
如果(realResponse.statusCode==200){
//真的是任何2**但例如
_下载数据=[[NSMutableData alloc]init];
NSLog(“良好响应”);
}否则{
NSLog(@“错误响应=%i”,realResponse.statusCode);
}
}
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据{
如果(连接==\u连接){
[_下载数据appendData:data];
NSLog(@“获取数据…”);
}
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接{
如果(连接==\u连接){
_连接=零;
NSLog(@“我们完成了,通知用户界面或代表”);
}
}
-(无效)连接:(NSURLConnection*)连接失败错误:(NSError*)错误{
_连接=零;
NSLog(@“噢,不!错误:%@”,Error.localizedDescription);
}
-(空)荷载{
NSURL*url=[NSURL URLWithString:@”http://www.google.com/"];
NSURLRequest*request=[NSMutableUrlRequestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
时间间隔:60];
//将强指针指定给新连接
_连接=[[NSURLConnection alloc]initWithRequest:request委托:self];
NSLog(@“连接已初始化”=%@,(!!u连接)@“是”:@“否”);
}
@结束
当您这样声明连接时:
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
您正在创建一个本地指针。当方法完成时,由于它是对NSURLConnection
的最后一个强引用,ARC
将其释放。您需要使用强ivar(和/或)属性来保存对所创建的NSURLConnection
的强引用
编辑
下面是我在一个示例项目中测试的基本代码示例。试试看。详细日志记录很有帮助
@implementation <#Your class here#> {
// With ARC ivars are strong by default
NSMutableData *_downloadedData;
NSURLConnection *_connection;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSHTTPURLResponse *realResponse = (NSHTTPURLResponse *)response;
if (realResponse.statusCode == 200){
// Really any 2** but for example
_downloadedData = [[NSMutableData alloc] init];
NSLog(@"Good response");
} else {
NSLog(@"Bad response = %i",realResponse.statusCode);
}
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
if (connection == _connection){
[_downloadedData appendData:data];
NSLog(@"Getting data...");
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
if (connection == _connection){
_connection = nil;
NSLog(@"We're done, inform the UI or the delegates");
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
_connection = nil;
NSLog(@"Oh no! Error:%@",error.localizedDescription);
}
- (void)load {
NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
NSURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
// Assign strong pointer to new connection
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"Connection was initialized? = %@",(!!_connection)?@"YES":@"NO");
}
@end
@实现{
//默认情况下,带弧的IVAR是强的
NSMutableData*_下载数据;
NSURLConnection*_连接;
}
-(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应{
NSHTTPURLResponse*realResponse=(NSHTTPURLResponse*)响应;
如果(realResponse)