无法创建请求:URL 错误

发布于 2024-12-29 21:54:20 字数 514 浏览 0 评论 0原文

当我尝试寻找这样的位置: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];

When i try to look for a location like this: Florida,USA it goes OK, but when i try it like this: Florida USA, i got that error:

Unable to create Request (bad url?)

The problem belongs with the spaces on the location taped, is there any way to resolve that?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

禾厶谷欠 2025-01-05 21:54:20

我认为问题是你需要对字符串进行 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

..fredrik

I think the problem is that you need to urlencode your string. A space character isn't a valid url string:

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

If NSASCIIStringEncoding doesn't work you could try: NSUTF8StringEncoding

..fredrik

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文