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

时间:2023-01-15 12:50:13

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. 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." 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. I've also tried:

我正在开发一个iOS应用程序,其中SoundCloud用户在Web视图中使用OAuth登录,然后该应用程序通过Alamofire向SoundCloud API发出HTTP请求。我已经成功验证了用户并存储了他们的令牌(使用ABMSoundCloudAPI),但是对https://api.soundcloud.com/me的GET请求失败,出现-1005错误,“网络连接丢失了。”这似乎是这里讨论的iOS的常见问题,但重置模拟器并不能解决我的问题,使用设备时也会出现问题。我也尝试过:

  • 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.}

1 个解决方案

#1


0  

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错误消失了。

#1


0  

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错误消失了。