3

我目前正在尝试使用 twitter 流 api,并且正在尝试使用NSURLConnection. 由于它不适用于 twitter,我简化了所有内容并尝试从 google 的网站上获取一些源代码,但这也不起作用。

连接开始和结束,但不调用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!");
}
4

3 回答 3

2

几个想法。

  1. 您的声明connectionDidFinishLoading看起来不正确。标准NSURLConnectionDataDelegate方法没有destinationURL参数:

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        NSLog(@"%s", __FUNCTION__);
    }
    
  2. 鉴于 的存在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];
        }
    }
    
  3. 顺便说一句,您不想start在使用initWithRequestor时调用方法connectionWithRequestinitWithRequest:delegate:startImmediately:仅当您执行简单操作并指示它不要 时才需要它startImmediately

  4. 无论如何,我使用了以下,它工作正常:

    - (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);
    }
    
于 2013-01-28T04:25:00.663 回答
0

您的 NSURLConnection 局部变量connectionviewDidLoad. 向您的 ViewController 添加一个属性以在范围内保存 NSURLConnection 变量。

于 2013-01-27T23:47:28.437 回答
0

这里有人说是他们的SOAP格式 NSURLConnection委托方法:didReceiveData not called ...Why ?? (iPhone SDK)

这也可能是您正在寻找的 NSURLConnection didReceiveData not called

于 2013-01-27T23:33:44.907 回答