initWithContentsOfURL 出现奇怪的错误

发布于 2024-08-17 01:53:34 字数 1124 浏览 1 评论 0原文

我目前正在从外部 API 读取一些 XML。

以下代码工作正常:

NSError *error = nil;

NSString *xmlString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];

但是,我想添加一些错误处理(“无法访问服务器”、“没有互联网连接”等)。因此,我在 Reachability 示例代码的帮助下将此块放置到位:

if ([error code] == kCFURLErrorNotConnectedToInternet) {
        NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedString(@"No Internet connection",@"Error message displayed when not connected to the Internet.") forKey:NSLocalizedDescriptionKey];
        NSError *noConnectionError = [NSError errorWithDomain:NSCocoaErrorDomain code:kCFURLErrorNotConnectedToInternet userInfo:userInfo];
        [self handleError:noConnectionError];
    } else {
        // otherwise handle the error generically
        [self handleError:error];
    }

handleError 方法仅显示包含错误详细信息的 UIAlertView。当我在没有连接的情况下运行应用程序时,我希望看到错误消息。然而,我得到的只是“操作无法完成(Cocoa 错误 256)”,据我所知,这是一个通用的读取错误。

起初我以为这可能只是 TouchXML 的方法,但正如你所看到的,我将其更改为 NSString 的 initWithCOntentsOfURL。

谁能阐明这个问题?

谢谢,

丰富

I'm currently working on reading some XML from an external API.

The following code works fine:

NSError *error = nil;

NSString *xmlString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];

However, I wanted to add some error handling ("The server cannot be reached", "No Internet connection" etc). So, I put this block in place with a little help from the Reachability sample code:

if ([error code] == kCFURLErrorNotConnectedToInternet) {
        NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedString(@"No Internet connection",@"Error message displayed when not connected to the Internet.") forKey:NSLocalizedDescriptionKey];
        NSError *noConnectionError = [NSError errorWithDomain:NSCocoaErrorDomain code:kCFURLErrorNotConnectedToInternet userInfo:userInfo];
        [self handleError:noConnectionError];
    } else {
        // otherwise handle the error generically
        [self handleError:error];
    }

The handleError method simply displays a UIAlertView with the error's details. When I ran the app with no connection, I expected to see my error message. However, all I ever get is "Operation could not be completed (Cocoa error 256)" which from what I can gather is a generic read error.

At first I thought it might just be TouchXML's method, but as you can see I changed this to NSString's initWithCOntentsOfURL.

Can anyone shed any light on this issue?

Thanks,

Rich

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2024-08-24 01:53:34

您看到除 kCFURLErrorNotConnectedToInternet 之外的其他错误。该错误消息不是来自您的自定义错误,而是由后台的某个对象生成的。您需要转储实际收到的错误的 userInfo 字典才能查看详细信息。我会记录它而不是使用警报来确保您获得所有信息。

如果 userInfo 字典实际上是空的,我将首先测试 url 以查看它是否实际返回任何内容。尝试初始化一个数据对象,看看会得到什么。这将缩小 URL 问题和将数据转换为字符串问题之间的范围。

You seeing some other error than kCFURLErrorNotConnectedToInternet. The error message is not coming from your custom error but is generated by some object under the hood. You need to dump the userInfo dict of the error you actually get to see the details. I would log it instead of using an alert to make sure you get all of it.

If the userInfo dict is actually empty, I would start by testing the url to see if it actually returns anything. Try initializing a data object and see what you get. That will narrow it down between a problem with the URL and a problem with converting the data into a string.

萌能量女王 2024-08-24 01:53:34

试试这个:
1)使用dataWithContentsOfURL创建NSData
2)只有当上面创建的NSData不为null时才创建xmlString,即通过检查if(data)

Try this:
1) Create NSData using dataWithContentsOfURL
2)No create xmlString only when NSData created above is not null, i.e passes check if(data)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文