json - 如何使用 Swift 和 Alamofire 连接 http 服务器

标签 json swift alamofire

您好,我是 swift 的新人,我遇到了服务器连接问题。我正在尝试使用方法 post 发出请求,以使用用户名和密码登录服务器。如何连接到服务器并进行身份验证?

错误消息:

 ["id": <null>, "error": {
    code = "-32700";
    message = "Parse error";
}, "jsonrpc": 2.0]

代码:

class ViewController: UIViewController {

override func viewDidLoad() {
   var userName = "root"
   var password = "admin01"

 //   http://192.168.1.1/ubus
   //var LOGIN_TOKEN = 0000000000000000
    let url: String = "http://192.168.1.1/ubus"
    let param: [String: Any] = ["username": "admin", "password": "admin01", "token": "0000000000000000"]
    Alamofire.request(url, method: .post, parameters: param,
                      encoding: JSONEncoding.default)
        .responseJSON { response in
            guard response.result.error == nil else {
                // got an error in getting the data, need to handle it
                print("error calling post")
                print(response.result.error!)
                return
            }

            // make sure we got some JSON since that's what we expect
            guard let json = response.result.value as? [String: Any] else {
                print("didn't get todo object as JSON from API")
                print("Error: \(response.result.error)")
                return
            }

            print(json)
    }


    super.viewDidLoad()
    // 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.
}

我需要获取服务器上的 Json:

{"jsonrpc":"2.0","id":1,"result":[0,{"ubus_rpc_session":"07e111d317f7c701dc4dfde1b0d4862d","timeout":300,"expires":300,"acls":{"access-group":{"superuser":["read","write"],"unauthenticated":["read"]},"ubus":{"*":["*"],"session":["access","login"]},"uci":{"*":["read","write"]}},"data":{"username":"root"}}]}

请求应如下所示:

"{ \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\": [ \"00000000000000000000000000000000\", \"session\", \"login\", { \"username\": \"root\", \"password\": \"admin01\"  } ] }"

最佳答案

尝试使用此代码:

 Alamofire.request(url, method: .post, parameters: parameters).responseJSON { response in
                    if(response.result.isFailure){
                        print("no data!");

                    }else{
                      print("received data!");

// make sure we got some JSON since that's what we expect
            guard let json = response.result.value as? [String: Any] else {
                print("didn't get todo object as JSON from API")
                print("Error: \(response.result.error)")
                return
            }

            print(json)
                    }
                }

关于json - 如何使用 Swift 和 Alamofire 连接 http 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41591855/

相关文章:

json - 如何读取多个json文件

javascript - 使用 Jinja 将数据作为 JSON 对象从 Python 发送到 Javascript

java Jackson Mapper - 嵌套属性

ios - UIBezierPath addClip 和 drawRect

ios - 无法在 iPad + Swift 3 上使用蓝牙扬声器进行录音

json - 快速解析 JSON 数据

PHP 切换对象是字符串还是数字

ios - 如何在 Xcode 10 中打开 Xcode 11 Beta 4 项目?

swift - Alamofire 返回错误

Swift Alamofire 响应状态码