Tag: alamofire

Swift 2.0 + Alamofire 3.0 – 缓慢加载JSON

我有一个展示健康食谱的项目。 该项目将JSON对象从Alamofire返回到Arraylist,然后返回Tableview中的Table Cell。 当我加载所有的对象到tableview它加载…很慢。 8+秒。 但是我的目标是3秒。 我知道这个问题是在alamofire周围,因为我检查打印(时间戳)。 任何我做错了? func getCookData(urlString: String , completionHandler: (String, String, String, String, String, String, String, String, String, String, String, String, String, String, String) -> ()) -> (){ Alamofire.request(.GET, urlString).responseJSON() { response in // var cookArray:[CookData] = [] if response.result.isSuccess { let data = response.result.value let cookJson = JSON(data!) for (var […]

Alamofire HTTP请求失败

我用Alamofire做了一些HTTP请求。 有些请求已经成功,有些请求失败。 错误是Invalid value around character 0. 失败的请求给了我上面的错误。 下面我提到了一个失败的示例代码。 let parameters = ["amount": ["10"], "payment_method": ["paypal"], "date": ["2015-11-25"], "details": ["Payment description"]] let headers = [ "Accept": "*/*", "Content-Type": "application/json" ] let url = "https://livetest.somedomain.com/api/invs/LAT1j5da99PdPg/payments?auth_token=pbtTEPNki3hUhGBuPX3d" Alamofire.request(.POST, url, parameters: parameters, encoding: .JSON, headers: headers) .responseJSON { response in let results = response.result print(results) print(response.debugDescription) } 请帮我find问题

tableView.reloadData()错误:对成员的模糊引用

我试图重新加载我的表中的数据,一旦成功的GET请求已经执行。 但是,当我写self.tableView.reloadData()时,我被引发这个错误: 成员'tableView(_:numberOfRowsInSection :)'的歧义引用 这是受影响的代码 override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() let url = "http://api.myawesomeapp.com" Alamofire.request(.GET, url).validate().responseJSON { response in switch response.result { case .Success(let data): let json = JSON(data) if let data = json["name"].arrayValue as [JSON]?{ self.datas = data self.tableView.reloadData() } case .Failure(let error): print("Request failed with error: \(error)") } } } 这是一个链接到完整的代码https://codeshare.io/eMqIx 我该如何解决这个错误?

Swift Alamofire从刷新令牌请求错误中获取令牌

我对使用Alamofire相当陌生,而且我正在用这个要求将我的头撞在墙上。 我正在使用GIDSignIn,并成功获取用户的令牌和刷新令牌,范围为[“ https://www.googleapis.com/auth/youtube.readonly ”]。 我试图完成这个请求,如网站上的示例所示。 该网站说,忽略使用iOS的client_secret ,我这样做。 POST /oauth2/v4/token HTTP/1.1 Host: www.googleapis.com Content-Type: application/x-www-form-urlencoded client_id=<your_client_id>& client_secret=<your_client_secret>& refresh_token=<refresh_token>& grant_type=refresh_token 以下是我如何用Alamofire实现它。 我的client_id是来自GoogleService-Info.Plist中CLIENT_ID键的值,这是一个以.apps.googleusercontent.com结尾的string。 refresh_token似乎也有我在网上看到的其他例子的正确格式。 let endpoint = "https://www.googleapis.com/oauth2/v4/token" let parameters = [ "client_id" : client_id, "refresh_token" : refresh_token, "grant_type" : "refresh_token" ] Alamofire.request(endpoint, method: .post, parameters: parameters, encoding: JSONEncoding.default) .responseJSON { (data) in print("data: \(data)") let json = […]

为什么alamofire下载警报的进度会闪烁,后面的屏幕开始变黑?

在使用alamofire下载时,我有警告的进度视图,但是问题是,当我想要下载alert时的进度视图将会开始完成,但闪烁,另一个问题是后面的屏幕将开始淡化为黑色每秒钟都会有一个新的提示,进度视图会显示,最后有很多提醒,我怎样才能避免它们? downloadTimer = Timer.scheduledTimer(timeInterval: 1 , target: self, selector: #selector(flashingDownload), userInfo: nil, repeats: true) let destination = DownloadRequest.suggestedDownloadDestination() Alamofire.download("http://example.com", to: destination).downloadProgress(queue: DispatchQueue.global(qos: .utility)) { (progress) in print("Progress: \(progress.fractionCompleted)") sixthLevelViewController.progressdownload = Float(progress.fractionCompleted) if sixthLevelViewController.progressdownload == 1.0 { self.downloadfinished = true let alertController = UIAlertController(title: "finish download ", message: " download Completed! ", preferredStyle: UIAlertControllerStyle.alert) let okAction […]

为什么void函数中出现意外的非void返回值?

我创build了一个从API获取URL的函数,并返回URLstring作为结果。 但是,Xcode给了我这个错误消息: void函数中意外的非void返回值 有谁知道为什么发生这种情况? func getURL(name: String) -> String { let headers: HTTPHeaders = [ "Cookie": cookie "Accept": "application/json" ] let url = "https://api.google.com/" + name Alamofire.request(url, headers: headers).responseJSON {response in if((response.result.value) != nil) { let swiftyJsonVar = JSON(response.result.value!) print(swiftyJsonVar) let videoUrl = swiftyJsonVar["videoUrl"].stringValue print("videoUrl is " + videoUrl) return (videoUrl) // error happens here […]

Alamofire错误更新到最新版本后

在更新到Alamofire 4.x的最新版本后,我遇到了一个问题,其中myRequest.response(第2行)给出错误“无法调用非函数types的值HTTPURLResponse?”。 我一直在使用这段代码没有任何问题。 有人可以帮忙吗? let queue = DispatchQueue(label: "com.cnoon.manager-response-queue", attributes: DispatchQueue.Attributes.concurrent) let myRequest = Alamofire.request(myURL, method: .get, headers: headers) myRequest.response ( queue: queue, responseSerializer: Request.JSONResponseSerializer(options: .allowFragments), completionHandler: { response in switch response.result { case .success: print(response.timeline) if let httpStatusCode = response.response?.statusCode { switch (httpStatusCode) { case 200: if let myValue = response.result.value { updateSuccessful = […]

带有Alamofire 4数据的POST请求

怎么可能用Alamofire 4发送POST请求与HTTP正文中的数据? 我使用自定义编码在迅速2.3它工作良好。 我转换我的代码swift 3,我试图参数编码,但不工作。 此代码: public struct MyCustomEncoding : ParameterEncoding { private let data: Data init(data: Data) { self.data = data } public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest { var urlRequest = try urlRequest.asURLRequest() do { urlRequest.httpBody = data urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type") } catch { throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error)) […]

Alamofire下载数据后的返回值

我有一个使用Alamofire下载数据的函数,然后我想返回这些数据。 现在我知道Alamofireasynchronous运行,为了返回数据,我应该使用completionHandler ,但是我不知道如何使用它。 由于我不是第一个遇到这个问题的人,所以我find了一些解决类似问题的办法,但是我不能把它们应用到我的案例中去。 这是我的代码: func downloadImageFromServer(imageUrl: String) -> (String, String) { var myData1 = String() var myData2 = String() Alamofire.request(.GET, imageUrl) .responseImage { response in switch response.result { case .Success: if let newImage = response.result.value { myData1 = //returned image name myData2 = //edited image name } case .Failure: //Do something } } return […]

Alamofire使用多部分/表单数据上传图像

我正在寻找一个有效的示例代码来使用Alamofire上载包含multipart / form-data的图像 我不能得到这个代码来处理我的项目 Alamofire.upload( .POST, URLString: "http://httpbin.org/post", multipartFormData: { multipartFormData in multipartFormData.appendBodyPart(fileURL: unicornImageURL, name: "unicorn") multipartFormData.appendBodyPart(fileURL: rainbowImageURL, name: "rainbow") }, encodingCompletion: { encodingResult in switch encodingResult { case .Success(let upload, _, _): upload.responseJSON { request, response, JSON, error in println(JSON) } case .Failure(let encodingError): println(encodingError) } } ) 此代码有此错误Could not find member 'POST' 有些人说,如果你使用Alamofire.Method.POST它将解决这个问题,但我得到了这个错误,而Cannot […]