无法创建请求:错误的URL

时间:2012-01-30 09:29:35

标签: objective-c ios google-maps

当我尝试寻找这样的位置时:Florida,USA它没问题,但是当我这样尝试时:Florida USA,我收到了这个错误:

Unable to create Request (bad url?)

问题属于录制位置上的空格,有什么方法可以解决这个问题吗?

NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true",theLocationString];
    //Create a URL object.
    NSURL *url = [NSURL URLWithString:urlString];
    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];

1 个答案:

答案 0 :(得分:8)

我认为问题是你需要对你的字符串进行urlencode。空格字符不是有效的网址字符串:

NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true",theLocationString];
//Create a URL object.
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:
                    NSASCIIStringEncoding]];
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];

如果NSASCIIStringEncoding不起作用,您可以尝试:NSUTF8StringEncoding

..弗雷德里克