2013-08-24 212 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。我將它添加到你的代碼和我的電腦上,它工作正常。 –