2013-08-24 106 views
0

我试图从页面获取XML,但NSURLConnection没有返回任何东西。NSURLConnection没有登录到控制台

- (void)downloadDataWithMission:(NSString *)mission 
{  
    // Create a new data container for the stuff that comes back from the service 
    xmlData = [[NSMutableData alloc] init]; 

    // Construct a URL that will ask the service for what you want 
    NSString *urlstring = [NSString stringWithFormat:@"http://www.google.com/"]; 

    // , mission, [self getCountry] 

    NSURL *url = [NSURL URLWithString:urlstring]; 

    // Put that URL into an NSURLRequest 
    NSURLRequest *req = [NSURLRequest requestWithURL:url]; 

    // Create a connection that will exchange this request for data from the URL 
    urlConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES]; 
} 

# pragma mark NSURLConnection 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    // This method is called when the server has determined that it 
    // has enough information to create the NSURLResponse. 

    // It can be called multiple times, for example in the case of a 
    // redirect, so each time we reset the data. 

    // receivedData is an instance variable declared elsewhere 
    [xmlData setLength:0]; 
} 


// This method will be called several times as the data arrives 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    // Add the incoming chunk of data to the container we are keeping 
    // The data always come in the correct order 
    [xmlData appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    // We are just checking to make sure we are getting the XML 
    NSString *xmlCheck = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding]; 

    NSLog(@"xmlCheck = %@", xmlCheck); 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    // Release the connection object, we're done with it 
    urlConnection = nil; 

    // Release the xmlData object, we're done with it 
    xmlData = nil; 

    // Grab the description of the error object passed to us 
    NSString *errorString = [NSString stringWithFormat:@"Connection Failed: %@", [error localizedDescription]]; 

    // Create and show an alreat view with this error displayed 
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

    [av show]; 
} 

@end 

为什么连接不工作?这是代表问题吗?在另一个项目中,一切正常。基础SDK在这些项目中是相同的 - iOS 6.1。

+0

这是你正在测试的实际代码吗?你断点/记录每种方法吗?有没有人叫? – Wain

+0

这是实际的代码。该代码只能在' - (void)downloadDataWithMission:(NSString *)mission'方法中使用。 – theShay

+0

设备/模拟器?你如何定义'urlConnection'(强属性)? – Wain

回答

1

一切都到这行完美的作品:

NSString *xmlCheck = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding]; 

但是它不能处理的编码,我认为。也许在谷歌有一个无效的UTF-8字符。试试NSASCIIStringEncoding,它会起作用。如果你想使用UTF-8,你可能需要深入了解为什么谷歌不符合UTF-8标准。

+0

即使在' - (void)连接:(NSURLConnection *)连接didReceiveData:(NSData *)data'方法中,NSLog'也不能工作。 – theShay

+0

对我来说,您应该知道某些xcode版本中存在一个错误,可能需要您在再次看到任何日志之前重新启动xcode和计算机。这是如果日志记录不工作。在你的代码中,didReceiveData中没有NSLog。我将它添加到你的代码和我的电脑上,它工作正常。 –