gpt4 book ai didi

json - 如何使用 Alamofire 从本地主机的 API 获取 JSON | swift

转载 作者:行者123 更新时间:2023-11-28 12:49:11 24 4
gpt4 key购买 nike

我想从本地获取JSON,这是我的代码

   Alamofire.request(.GET, "localhost/speex/service.php")
.responseJSON { response in
debugPrint(response)
}

但它向我发送了这个错误

[Request]: <NSMutableURLRequest: 0x7fc53960dda0> { URL: localhost/speex/service.php }
[Response]: nil
[Data]: 0 bytes
[Result]: FAILURE: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSUnderlyingError=0x7fc539567ad0 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, NSErrorFailingURLStringKey=localhost/speex/service.php, NSErrorFailingURLKey=localhost/speex/service.php, NSLocalizedDescription=unsupported URL}
[Timeline]: Timeline: { "Request Start Time": 484405610.886649, "Initial Response Time": 484405612.654189, "Request Completed Time": 484405612.654189, "Serialization Completed Time": 484405612.654278, "Latency": 1.76753997802734 secs, "Request Duration": 1.76753997802734 secs, "Serialization Duration": 8.89897346496582e-05 secs, "Total Duration": 1.76762896776199 secs }

PD:当我打开 url“localhost/speex/service.php”时,我可以正确看到 JSON 代码

最佳答案

本地主机无法在 iOS 模拟器或设备中访问。您需要为本地主机设置本地 IP 地址,例如:http://192.168.1.100然后在您的程序中使用此 IP 地址。

关于json - 如何使用 Alamofire 从本地主机的 API 获取 JSON | swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37100478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com