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

标签 json swift networking localhost alamofire

我想从本地获取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/

相关文章:

java - 使用 gson 将 JSON 转换为 java 对象

python - 将 JSON 响应转换为 pandas 数据帧

swift - Swift 2 方向步骤循环的语法

HTTP 请求和 TCP 连接

security - SYN Cookie 网络服务器安全

node.js - 在 Jenkins 中使用 jq 在命令行上使用 JSON

javascript - 如何使用 Nunjucks 安全地将 JSON 渲染到内联 &lt;script&gt; 中?

ios - UILocalNotification 每周重复一次

string - 如何从字符串中删除特定字符之后的字符?

java - 高性能网络的 Netty 替代品是什么?