首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CFNetwork错误:由于格式错误,连接失败

CFNetwork错误:由于格式错误,连接失败
EN

Stack Overflow用户
提问于 2012-09-12 07:57:58
回答 1查看 972关注 0票数 1

在运行iphone应用程序时,我多次收到以下错误

代码语言:javascript
复制
The connection failed due to a malformed URL

CFNetwork Error Codes Reference,我得到了CFNetwork触发的错误信息

  • 何时以及为什么会触发此错误?
  • 如何消除这一错误?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-12 08:34:51

NSURLConnection有一个方法canHandleRequest,它将返回一个布尔值,不管url是否有效。

代码语言:javascript
复制
NSString *strUrl = [NSString stringWithFormat:@"http://test.com/stopBoard/%i",busStopCode];
NSURL *url = [NSURL URLWithString:strUrl];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url 
                                              cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData 
                                          timeoutInterval:20.0];
// a cache policy of NO must be used otherwise the previous query is used if the phone is not online

BOOL canGo = [NSURLConnection canHandleRequest:request];
if(canGo){
    connection = [[NSURLConnection alloc] initWithRequest:request 
                                                 delegate:self 
                                         startImmediately:YES];


} else {
    self.errorMessage = @"\n\nurl check failed\n\n";
    return NO;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12383602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档