我正在尝试使用twitter流式传输API,我正在尝试使用NSURLConnection
获取流。因为它不适用于Twitter,我简化了一切,并试图从谷歌的网站上获取一些源代码,但这不起作用。
连接开始和结束,但不调用didReceiveData
委托。我确定我错过了什么。希望你的家伙可以帮助我!
标题:@interface ViewController : UIViewController <NSURLConnectionDataDelegate, NSURLConnectionDelegate, NSURLConnectionDownloadDelegate, NSURLAuthenticationChallengeSender>
在身体里:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLConnection *connection;
NSMutableURLRequest *request;
// Do any additional setup after loading the view, typically from a nib.
request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[request setHTTPMethod:@"GET"];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
- (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL {
NSLog(@"Stream finished.");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", dataString);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"Connection failed!");
}
答案 0 :(得分:2)
一些想法。
您对connectionDidFinishLoading
的声明看起来不正确。标准NSURLConnectionDataDelegate
方法没有destinationURL
参数:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"%s", __FUNCTION__);
}
鉴于存在NSURLAuthenticationChallengeSender
,如果您预计会遇到质疑(Google网站无法提供此问题),那么您显然会相应地处理它:
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
// For example, if you have a userid/password to use, see if this is the first
// challenge, and then tell NSURLConnection to try using those credentials, and if
// it failed a second time, you might just cancel the authentication challenge.
if (challenge.previousFailureCount == 0) {
NSURLCredential *credential = [NSURLCredential credentialWithUser:kUserID password:kPassword persistence:NSURLCredentialPersistenceForSession];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
} else {
[challenge.sender cancelAuthenticationChallenge:challenge];
}
}
顺便说一下,当您使用initWithRequest
或connectionWithRequest
时,您不想调用start
方法。只有在您执行简单initWithRequest:delegate:startImmediately:
并指示不 startImmediately
时才需要它。
无论如何,我使用了以下内容并且工作正常:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"%s", __FUNCTION__);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%s: %@", __FUNCTION__, dataString);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"%s error=%@", __FUNCTION__, error);
}
答案 1 :(得分:0)
有人在这里说这是他们的SOAP格式 NSURLConnection delegate method: didReceiveData not called ...Why ?? (iPhone SDK)
这也可能是您正在寻找的 NSURLConnection didReceiveData not called
答案 2 :(得分:0)
您的NSURLConnection局部变量connection
超出viewDidLoad
末尾的范围。向ViewController添加属性以将NSURLConnection变量保存在范围内。