0

我遇到了这个问题,我在这里浏览了很多关于堆栈溢出和其他地方的帖子,但找不到解决方案。我也在主线程上运行它。代码如下。

@interface JsonViewController : UIViewController <UIActionSheetDelegate,UIWebViewDelegate,NSURLConnectionDelegate>
{
  ....
}
@implementation JsonViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
    rssFeedDetailViewConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    NSLog(@"PRINTING RSS FEED %@", rssFeedDetailViewConnection);
[rssFeedDetailViewConnection scheduleInRunLoop:[NSRunLoop mainRunLoop] 
                                       forMode:NSDefaultRunLoopMode];
    [rssFeedDetailViewConnection start];
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Hello");
responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
     NSLog(@"Hello");
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
     NSLog(@"Hello");
    [responseData release];
    [connection release];
// Show error message
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
 NSLog(@"Hello");
    // Use responseData
    [responseData release];
    [connection release];
 }

任何帮助将不胜感激。我已经坚持了两天了..

4

2 回答 2

1

您不需要这些语句:

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

因为你正在使用initWithRequest:delegate:. 此调用已经开始加载数据。

如果删除这些特定语句会发生什么?

有关NSURLConnection的更多信息。

于 2012-08-21T11:17:37.563 回答
0

有一个名为finished的布尔值,并将此代码添加到您编写NSURLConnection代码的地方。

 while(!finished) {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

在你的 connectionDidFinishLoading 中添加这个

 finished = TRUE; 

那行得通。它不是我理解的最佳解决方案,但它有效。

于 2012-08-21T15:46:49.773 回答