2017-01-09 70 views
0

要解決我的問題,斯威夫特3,我試圖發送一個請求到服務器,並獲得JSON,但我得到:施工太複雜在合理的時間

Construction was too complex to be solved in reasonable time.

我想盡辦法,但它不起作用。

var userName = "root" 
var password = "admin01" 
//var LOGIN_TOKEN = 0000000000000000 

let parameters = [ 
    "{\n", 
    " \"jsonrpc\": \"2.0\",\n", 
    " \"id\": \"1\",\n", 
    " \"method\": \"call\",\n", 
    " \"params\": [\n", 
    "  \"0000000000000000\",\n", 
    "  \"session\",\n", 
    "  \"login\",\n", 
    "  {\n", 
    "   \"username\": \"" + userName + "\",\n", 
    "   \"password\": \"" + password + "\"\n", 
    "  }\n", 
    " ]\n", 
    "}" 
] 

let joiner = "" 
let joinedStrings = parameters.joined(separator: joiner) 
print("joinedStrings: \(joinedStrings)") 

// All three of these calls are equivalent 
Alamofire.request("http://192.168.1.1", method: .post, parameters: parameters).responseJSON { response in 
    print("Request: \(response.request)") 
    print("Response: \(response.response)") 


    if let JSON = response.result.value { 
     print("JSON: \(JSON)") 
    } 
} 

現在我tryed來創造DIC並轉換爲JSON,但在那之後,我得到要求的問題我有我的聲明參數。他們說:使用未解決的標識符dictFromJSON

var userName = "root" 
    var password = "admin01" 
    //var LOGIN_TOKEN = 0000000000000000 

    let jsonObject: [String: Any] = 
     ["jsonrpc" : 2.0, 
     "id": 1, 
     "method": "call", 
     "params": [ "00000000000000", 
        "session", 
        "login", 
        [ "username": userName, 
         "password": password]], 
     ] 
    do { 
     let jsonData = try JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted) 
     // here "jsonData" is the dictionary encoded in JSON data 

     let decoded = try JSONSerialization.jsonObject(with: jsonData, options: []) 
     // here "decoded" is of type `Any`, decoded from JSON data 

     // you can now cast it with the right type 
     if let dictFromJSON = decoded as? [String:String] { 
      // use dictFromJSON 
     } 
    } catch { 
     print(error.localizedDescription) 
    } 




    // All three of these calls are equivalent 
    Alamofire.request("http://192.168.1.1/ubus", method: .post, parameters: dictFromJSON).responseJSON { response in 
     print("Request: \(response.request)") 
     print("Response: \(response.response)") 
+2

不要手動反正創建一個JSON字符串。創建數組和字典然後將它們轉換爲JSON。 http://stackoverflow.com/a/31263337/2227743 – Moritz

+0

現在我得到錯誤:在調用中額外的參數'方法'。你能幫我解決嗎? –

+0

這只是Swift 2中的一個例子。重要的是這個想法。你可以自己找到其他的例子,有很多。 – Moritz

回答

0

更新:

根據Alamofire的文件,(你可以看到here),你不需要參數字典轉換成JSON。

例如,

let parameters: Parameters = [ 
    "foo": "bar", 
    "baz": ["a", 1], 
    "qux": [ 
     "x": 1, 
     "y": 2, 
     "z": 3 
    ] 
] 

// All three of these calls are equivalent 
Alamofire.request("https://httpbin.org/post", parameters: parameters) 
Alamofire.request("https://httpbin.org/post", parameters: parameters, encoding: URLEncoding.default) 
Alamofire.request("https://httpbin.org/post", parameters: parameters, encoding: URLEncoding.httpBody) 

// HTTP body: foo=bar&baz[]=a&baz[]=1&qux[x]=1&qux[y]=2&qux[z]=3 

OLD:

您應該使用百科的實際參數。

因此,與其宣稱像你這樣的參數,你應該這樣做:

let parameters = ["jsonrpc" : 2.0, 
       "id": 1, 
       "method": "call", 
       "params": [ "00000000000000", 
          "session", 
          "login", 
          [ "username": userName, 
          "password": password]], 
      ] 
+0

我嘗試過這種方式,但現在我遇到了問題,請更新我的問題,請檢查並幫助我 –