【问题标题】:How to connect http server using Swift and Alamofire如何使用 Swift 和 Alamofire 连接 http 服务器
【发布时间】:2017-05-26 07:14:28
【问题描述】:

您好,我是 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\"  } ] }"

【问题讨论】:

  • 尝试将 let param: [String: Any] 改为 let param: [String: String]
  • 试过这几次,没有帮助
  • 你试过在浏览器中点击你的网址吗?
  • 基本上这个 url 是路由器和 url 不能在浏览器上工作,但是使用 android 请求它可以完美地工作

标签: json swift alamofire


【解决方案1】:

尝试使用此代码:

 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,我得到相同的错误代码 = "-32700"; message = "解析错误"; }; id = ""; jsonrpc = "2.0";
  • @GediminasUrbonas 然后您需要正确检查您的 JSON 格式。解析错误表示解析 JSON 时出现问题。
  • 我将在我的问题请求中添加我应该得到的终端和 JSon,你能帮我解决这个问题吗?
猜你喜欢
  • 1970-01-01
  • 2015-11-03
  • 1970-01-01
  • 2017-07-25
  • 2017-03-31
  • 1970-01-01
  • 2021-01-07
  • 2017-12-13
  • 1970-01-01
相关资源
最近更新 更多