iOS开发:为什么我的NSURLConnection失败并出现“错误的URL”只有部分用户出错? [英] iOS Develoment: Why is my NSURLConnection failing with a "bad URL" error for only some users?

查看:149
本文介绍了iOS开发:为什么我的NSURLConnection失败并出现“错误的URL”只有部分用户出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iOS应用程序从我的Rails 3应用程序(在Heroku上托管)请求 JSON 数据,它在我的设备和许多其他用户上运行良好,除了一个。我有一个用户告诉我我的应用程序无法检索JSON数据,所以我让她发给我一些日志数据,日志显示 NSURLConnection 委托方法<$正在调用c $ c> didFailWithError ,错误描述为bad URL。为什么会出现这种错误?为什么它只发生在某些设备而不是所有设备上?

I have an iOS app that requests JSON data from my Rails 3 app, hosted on Heroku, and it works great on my device and for many other users, except one. I have one user who has told me that my app fails to retrieve the JSON data, so I had her send me some log data and the log showed the NSURLConnection delegate method didFailWithError is being called and the error description reads "bad URL". Why is this error occurring and why is it ONLY occurring on just some devices instead of all devices?

这是我的代码,

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

并且日志显示...

bad URL - (null) - (null) - (null)

非常感谢您的所有智慧!

Thanks so much for all your wisdom!

推荐答案

看起来你不是在编码用户名。用户名中是否有空格或其他特殊字符?查看使用 NSString 方法 stringByAddingPercentEscapesUsingEncoding:

It doesn't look like you are encoding the username. Are there spaces or other special characters in the username? Look into using the NSString method stringByAddingPercentEscapesUsingEncoding:.

这篇关于iOS开发:为什么我的NSURLConnection失败并出现“错误的URL”只有部分用户出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆