【问题标题】:CFNetwork Error : The connection failed due to a malformed URLCFNetwork 错误:由于 URL 格式错误,连接失败
【发布时间】:2012-09-12 07:57:58
【问题描述】:

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

The connection failed due to a malformed URL

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

  • 何时以及为何触发此错误?
  • 如何摆脱这个错误?

【问题讨论】:

标签: iphone ios debugging cfnetwork


【解决方案1】:

NSURLConnection 有一个 canHandleRequest 方法,无论 url 是否有效,它都会返回一个布尔值,例如

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;
}

【讨论】:

    猜你喜欢
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 2018-11-18
    • 2015-11-30
    • 2019-05-27
    • 1970-01-01
    • 2021-07-02
    相关资源
    最近更新 更多