10

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!

Rahul Patel
  • 5,858
  • 6
  • 46
  • 72
BeachRunnerFred
  • 18,070
  • 35
  • 139
  • 238
  • Are the usernames of those who experience crashes/failures looking any suspicious? – Till May 28 '11 at 15:45
  • look at my answer for the more detailed answer : http://stackoverflow.com/questions/6287230/bad-url-when-requesting-with-nsurl/10292920#10292920 – YogiAR Oct 30 '13 at 10:45

4 Answers4

30

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

Jim
  • 72,985
  • 14
  • 101
  • 108
8

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` 
sergio
  • 68,819
  • 11
  • 102
  • 123
1

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.

Johnny Rockex
  • 4,136
  • 3
  • 35
  • 55
-1

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.

  • Actully Jim already gave the same answer, I don't think you are providing any new information. – ılǝ Sep 02 '13 at 12:41