[英]SoundCloud API: GET request fails with code -1005, using iOS/Alamofire
I'm working on an iOS app where SoundCloud users log in with OAuth in a web view and then the app makes HTTP requests to the SoundCloud API via Alamofire. 我正在使用一个iOS应用程序,其中SoundCloud用户在Web视图中使用OAuth登录,然后该应用程序通过Alamofire向SoundCloud API发出HTTP请求。 I've successfully authenticated the user and stored their token (using ABMSoundCloudAPI ), but GET requests to https://api.soundcloud.com/me are failing with a -1005 error, "The network connection was lost." 我已经成功验证了用户身份并存储了他们的令牌(使用ABMSoundCloudAPI ),但是对https://api.soundcloud.com/me的 GET请求失败,并出现-1005错误,“网络连接丢失。” This seems to be a common problem with iOS as discussed here , however resetting the simulator doesn't solve the problem for me and the problem also occurs when using a device. 这似乎是与iOS的共同问题为讨论在这里 ,但是重置模拟器不解决这个问题,我和使用设备时也会出现问题。 I've also tried: 我也尝试过:
I see the same error in every case. 在每种情况下,我都会看到相同的错误。 Are there other headers I should try? 还有其他标题应尝试吗? I'm using these libraries via Cocoapods: 我正在通过Cocoapods使用这些库:
Here is my code: 这是我的代码:
var retryCount = 0
func getUserInfo(token:String) {
let headers = ["Connection": "Close"]
Alamofire.request(.GET, "https://api.soundcloud.com/me?oauth_token=\(token)", parameters: ["client_id":clientId], encoding: .JSON, headers: headers)
.responseJSON { response in
guard response.result.error == nil else {
print("error calling GET on /me")
print(response.result.error)
if self.retryCount < 2 {
if let token = self.defaults.stringForKey("sc_key_token") {
self.getUserInfo(token)
++self.retryCount
}
}
return
}
guard let value = response.result.value else {
print("Error: did not receive data")
return
}
let user = JSON(value)
print("User info: " + user.description)
}
}
Error message: 错误信息:
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x126248c10 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://api.soundcloud.com/me?oauth_token=USER_TOKEN, NSErrorFailingURLKey=https://api.soundcloud.com/me?oauth_token=USER_TOKEN, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.}
It seems that this was caused by the request encoding. 看来这是由请求编码引起的。 When I switched from .JSON to .URL, the 1005 error went away. 当我从.JSON切换到.URL时,1005错误消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.