【问题标题】:How to avoid URL NOT SUPPORTED error on iphone如何避免 iphone 上的 URL NOT SUPPORTED 错误
【发布时间】:2011-10-18 17:03:43
【问题描述】:

我正在通过 POST HTTP 方法调用 URL。当我没有连接到 3G 或 WiFi 时,它永远不会正确获取 URL,也永远不会正确获取来自 URL 的响应。 通过帖子建立联系:

  NSString *URL = [NSString stringWithFormat:@"http://www.abc.com/webservices/client_server.cfc];
[[NSURLCache sharedURLCache] removeAllCachedResponses];

NSString *time_stamp =[NSString stringWithFormat:@"time_stamp=%@", self.today];

NSData *postData = [time_stamp dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

NSMutableURLRequest *URLRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[URLRequest setURL:[NSURL URLWithString:URL]];
[exhURLRequest setHTTPMethod:@"POST"];
[URLRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[URLRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[URLRequest setHTTPBody:postData];


self.connection =[[[NSURLConnection alloc] initWithRequest:URLRequest delegate:self] autorelease];

NSAssert(self.connection != nil, @"Failure to create URL connection.");

我正在检查我是否通过以下方式获得回复:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// check for HTTP status code for proxy authentication failures
// anything in the 200 to 299 range is considered successful

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

if (([httpResponse statusCode]/100) == 2) {
    self.data = [NSMutableData data];

} else {
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:
                              NSLocalizedString(@"HTTP Error",
                                                @"Error message displayed when receving a connection error.")
                                                         forKey:NSLocalizedDescriptionKey];
    NSError *error = [NSError errorWithDomain:@"HTTP" code:[httpResponse statusCode] userInfo:userInfo];
    [self handleError:error];
}
}

但是当 URL 未连接时,可以说当我在地铁或其他地方时,我无法连接到 URL,它会抛出“不支持 URL”。我怎样才能避免这种情况并允许应用程序向前推进而不抛出这样的错误?如果有人使用离线重定向到应用程序而没有继续在前屏幕上等待用户。

【问题讨论】:

  • 我不确定我是否在关注。你说“当我没有连接到 3G 或 Wifi 时,它永远不会正确获取 URL”。你的意思是当没有网络连接时?此外,您可能希望在创建请求的位置发布代码。
  • @onnoweb 我已经编辑了我的问题。

标签: iphone objective-c nsurlconnection nsurl


【解决方案1】:

下载苹果的可达性示例代码。将 Reachability.h+.m 添加到您的项目中。链接 SystemConfiguration 框架。然后像这样使用他们的 Reachability 类。

#import "Reachability.h"

WiFi:

-(void)doStuff{
    Reachability *wifi = [Reachability reachabilityForLocalWiFi];
    if (wifi.currentReachabilityStatus == ReachableViaWiFi){
        // do connected stuff
    }
    else {
        // do offline stuff
    }
}

对于 Wifi 或 3G:

-(void)doStuff{
    NetworkStatus connectivity = [Reachability reachabilityForInternetConnection].currentReachabilityStatus;
    if (connectivity == ReachableViaWiFi || connectivity == ReachableViaWWAN){
        // do connected stuff
    }
    else {
        // do offline stuff
    }
}

如果您将持续执行网络任务,请将可达性对象添加到您的类中。这将提高性能并允许您使用它的通知程序。

【讨论】:

    【解决方案2】:

    有时这个“URL NOT SUPPORTED 错误”是由于 url 未编码。

    使用 this.or 对这个编码 url 进行编码,或者互联网上有很多方法。 不要对整个 url 或基本 url 进行编码(因为它也会对 ":" 和 '/' 进行编码,并且可能会导致错误) 只有特殊字符需要编码,如 &,@ 等。

    NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
                                                                                                        NULL,
                                                                                                        (CFStringRef)pathRequest,
                                                                                                        NULL,
                                                                                                        (CFStringRef)@"!*'();:@&=+$,%#[]",
                                                                                                        kCFStringEncodingUTF8 ));
    

    【讨论】:

      【解决方案3】:

      How to check for an active Internet connection on iOS or OSX?

      使用此处提出的解决方案避免在没有有效互联网连接时发送请求。很简单。

      【讨论】:

      • 是的,在我所有需要互联网连接的应用程序中。如果你不能很好地处理连接,Apple 会拒绝你的应用从 Appstore 中移除。
      • 感谢您的信息。但是如果没有建立连接,它会捕获错误并让我继续前进吗?
      • 一般会防止你描述的错误发生。
      • 但我主要关心的是在没有连接的情况下不阻止而是允许应用程序继续前进。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 2018-09-05
      • 1970-01-01
      • 2013-03-29
      • 1970-01-01
      • 2022-12-15
      • 1970-01-01
      相关资源
      最近更新 更多