iOS Develoment: Why is my NSURLConnection failing with a “bad URL” error for only some users?

依然范特西╮ 提交于 2019-11-27 02:02:20

问题


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?

Here's my code,

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

and the log shows...

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

Thanks so much for all your wisdom!


回答1:


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:.




回答2:


It is likely that this depends on the specific URL containing characters that are not allowed for a URL.

You can try and escape the URL:

- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding` 



回答3:


I'm not sure how if at all it relates to your issue, but changing the Region in the Settings App had an affect on my app's ability to download. It was spitting out the same Bad URL error if the region was not set to United States. It might explain why some users are getting errors and not others.




回答4:


This kind of issue may will come due to argument data having special characters(ie. Andy & Co). SOAP automatically will reject this request and returns Bad URL or Bad Request. So, before you sending the data please ensure the data having special characters like &,<,>... If you found & in your string data , replace with <&>amp; to that string(eg.Andy <&>amp; Co). Hope this input will help for you.

Note: Before you use,Please remove start(<>) and end tag with that symbol.



来源:https://stackoverflow.com/questions/6162653/ios-develoment-why-is-my-nsurlconnection-failing-with-a-bad-url-error-for-onl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!