【问题标题】:Alamofire .GET returning nil for response and JSONAlamofire .GET 为响应和 JSON 返回 nil
【发布时间】:2015-01-11 03:24:38
【问题描述】:

此代码为响应和 json 返回 nil,知道问题出在哪里吗? url 是指向 api 端点的链接

import UIKit
import Alamofire


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        loadData()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func loadData(){

        let url = "http://httpbin.org/get"

        Alamofire.request(.GET, url).responseJSON { (request, response, json, error) in
            println(request)
            println(response)
            println(json)



    }



    }
}

它返回这个

{ 网址:http://httpbin.org/get } 零 无

【问题讨论】:

  • 错误告诉你什么?

标签: swift alamofire


【解决方案1】:

请务必检查您的error 案例。如果发生错误,Alamofire 将确保将其冒泡到您的响应序列化程序。您的请求失败,由于您没有打印出错误或检查错误,您不知道这是问题所在。如果您将loadData 方法更新为以下内容,您将更好地了解如何继续。

func loadData() {
    let request = Alamofire.request(.GET, "http://httpbin.org/get")
        .responseJSON { (request, response, json, error) in
            println(request)
            println(response)
            println(json)
            println(error)
    }

    debugPrintln(request)
}

如果error 消息没有为您提供足够的信息,那么您可以转到由debugPrintln 消息生成的cURL 命令。

【讨论】:

  • 嗨@Maxwell,你最终解决了你的问题吗?如果我的回答对您有所帮助,您能否将其标记为优秀的社区用户?干杯。