我有一个iOS应用程序,它JSON从我的Rails 3应用程序请求数据,托管在Heroku上,它在我的设备和许多其他用户(除了一个)之外运行良好.我有一个用户告诉我我的应用程序无法检索JSON数据,所以我让她发送了一些日志数据,日志显示正在调用NSURLConnection委托方法didFailWithError,错误描述显示为"错误的URL".为什么会出现此错误?为什么它只发生在某些设备而不是所有设备上?
这是我的代码,
-(void)getTournamentInfoWithUsername:(NSString*)username
{
NSString *urlString = [NSString stringWithFormat:@"http://myapp-tourney.heroku.com/tournaments/next.json?username=%@", username];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
[self setUrlConnection:[[NSURLConnection alloc] initWithRequest:request delegate:self]];
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
[MyLog logError:[NSString stringWithFormat:@"%@ - %@ - %@ - %@", [error localizedDescription], [error localizedFailureReason], [error localizedRecoveryOptions], [error localizedRecoverySuggestion]]];
}
Run Code Online (Sandbox Code Playgroud)
并且日志显示......
bad URL - (null) - (null) - (null)
Run Code Online (Sandbox Code Playgroud)
非常感谢您的所有智慧!
我有这个令人困惑的错误。我通过 GET 方法发送 JSON,网站将解析并显示数据。问题是我收到错误“NSURLErrorDomain Code -1000”或更简单的“Bad URL”。问题是,当我检查服务器时,我发送的数据已成功解析并显示。所以我真的很困惑为什么我会收到这个“错误的 URL”错误。有人能帮我吗?
这是我收到的错误:
错误域=NSURLErrorDomain代码=-1000“错误的URL”UserInfo=0xff73df0 {NSUnderlyingError=0xff73810“错误的URL”,NSLocalizedDescription=错误的URL}
编辑:
http://sample.com/imwebjson.php?sid=5amp13&job=sendNotes&im_flds={\"im_uid\":"1",\"im_bookid\":"57",\"im_pagenr\":"1",\"im_notes\":"Testing%5C%5Cn"}
Run Code Online (Sandbox Code Playgroud)
好吧,您可能会问为什么 JSON 字符串的某些部分已经被编码了。这些编码部分是特殊字符。我意识到 stringByAddingPercentEscapesUsingEncoding 非常不完整。它不会对所有特殊字符进行编码,更重要的是,当它对某些特殊字符进行编码时,URL根本无法被识别。所以我决定手动将特殊字符编码到字符串中。