2016-05-08 15 views
0

我想从本地主机获得JSON,这是我的代码如何从Alamofire的本地主机API获取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“本地主机/ Speex语音/ service.php “我可以正确看到JSON代码

回答

2

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