不调用NSURLConnection委托方法 [英] NSURLConnection delegate methods are not called

查看:75
本文介绍了不调用NSURLConnection委托方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的NSURLConnection以使用GET请求与服务器进行通信.连接效果很好,但是从未调用过NSURLConnection的委托方法.

I am trying to create a simple NSURLConnection to communicate with a server using a GET request. Connection works well, but delegates methods of NSURLConnection are never called..

这是在做什么:

NSString *post = [NSString stringWithFormat:@"key1=%@&key2=%@&key3=%f&key4=%@", val1, val4, val3, val4];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease] ;

[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.domain.com/demo/name/file.php?%@", post]]];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];

已经实现了以下委托方法,但是没有一个被调用..

Have implemented the following delegate methods, but none of them is called..

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"did fail");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    NSLog(@"did receive data");
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"did receive response ");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"did finish loading");
    [connection release];
}

我想念什么吗?

推荐答案

尝试在主线程上运行该操作:

Try running the operation on main thread:

NSURLConnection * connection = [[NSURLConnection alloc] 
                                initWithRequest:request
                                       delegate:self startImmediately:NO];

[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] 
                      forMode:NSDefaultRunLoopMode];
[connection start];

这篇关于不调用NSURLConnection委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆