简体   繁体   English

SoundCloud API:使用iOS / Alamofire的GET请求失败,代码为-1005

[英]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: 我也尝试过:

  • Removing and re-adding the wifi network 删除并重新添加wifi网络
  • Retrying the request programmatically if it fails 如果失败,则以编程方式重试请求
  • Adding a header with "Connection": "Close" 用“连接”添加标题:“关闭”

I see the same error in every case. 在每种情况下,我都会看到相同的错误。 Are there other headers I should try? 还有其他标题应尝试吗? I'm using these libraries via Cocoapods: 我正在通过Cocoapods使用这些库:

  • ABMSoundCloudAPI (0.2.1) ABMSoundCloudAPI(0.2.1)
  • AFNetworking (2.6.1) AFNetworking(2.6.1)
  • AFOAuth2Manager (2.2.0) AFOAuth2Manager(2.2.0)
  • Alamofire (3.1.2) Alamofire(3.1.2)
  • SwiftyJSON (2.3.1) SwiftyJSON(2.3.1)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM