gpt4 book ai didi

ios - NSURLConnection 未记录到控制台

转载 作者:行者123 更新时间:2023-11-29 03:41:12 24 4
gpt4 key购买 nike

我正在尝试从页面获取 XML,但 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。

最佳答案

到这一行的一切都完美运行:

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

但是它不处理我认为的编码。也许谷歌上有一些无效的 UTF-8 字符。尝试使用 NSASCIIStringEncoding 来代替,它会起作用。如果您想使用 UTF-8,您可能需要深入了解为什么 google 不兼容 UTF-8。

关于ios - NSURLConnection 未记录到控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18417310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com