2016-11-23 34 views
0

我有帖子請求和解析響應json代碼,但我的代碼不工作。Swift 3請求帖子,回覆返回無

我的類代碼

@discardableResult 
    open func getLoginMethod(_ method: String, parameters: String, completionHandler: @escaping (_ login: [Login]?, _ error: Error?) -> Void) -> URLSessionTask! { 
     let session = URLSession.shared 
     guard let url = NSURL(string: parameters) else { 
      completionHandler(nil, myErrors.InvalidUrlError) 
      return nil 
     } 

     let request = NSMutableURLRequest(url: url as URL) 
     request.httpMethod = "POST" 
     let sendingdata = "Simple=1" 
     request.httpBody = sendingdata.data(using: String.Encoding.utf8); 

     let task = session.dataTask(with: request as URLRequest) { data, response, error in 

      if error != nil { 
       completionHandler(nil, myErrors.getError) 
      } else { 
       do { 
        let login = try self.getLogin(jsonData: data! as NSData) 

        print(login) 

        completionHandler(login, nil) 
       } catch { 
        completionHandler(nil, error) 
       } 
      } 
     } 
     task.resume() 

     return task 
    } 

當我點擊動作按鈕的代碼給我只能

輸出。我在哪裏做錯了任何想法?

+0

@Vinodh耶確保模型的代碼實現。我認爲request.httpBody = sendingdata.data錯誤(使用:String.Encoding.utf8);或如果讓jsonArray =嘗試JSONSerialization.jsonObject(與:jsonData作爲數據,選項:.allowFragments)爲? [字符串:AnyObject] { – SwiftDeveloper

+0

@Vinodh我也加入了登錄模式 – SwiftDeveloper

+0

@Vinodh nope仍然相同 – SwiftDeveloper

回答

1

請找到下面的代碼使用我這是工作的罰款與當地son.Since你沒有提供網址和其他細節

if let file = Bundle.main.path(forResource: "response", ofType: "json") { 
       do { 
        let jsonData = try Data(contentsOf: URL(fileURLWithPath: file)) 
        let login = try self.getLogin(rawData: jsonData) 

        let arr = login.first 

        if let date = arr?.ServerCurrentDate { 

         print("Comming Date = \(date)") 


        } 

       } 
       catch{ 
        print(error) 
       } 
      } 


private func getLogin(rawData: Data) throws -> [Login] { 
     var login = [Login]() 

     do { 
      if let jsonDict = try JSONSerialization.jsonObject(with: rawData, options: .allowFragments) as? [String: AnyObject] { 
       print(jsonDict) 
       if let result = jsonDict["Result"] as? [String : AnyObject] 
       { 
        var properties = [String: AnyObject]() 
        properties["IsSuccess"] = result["IsSuccess"] as? Bool as AnyObject? 
        properties["MemberId"] = result["MemberId"] as? Int as AnyObject? 
        properties["Name"] = result["Name"] as? String as AnyObject? 
        properties["Image"] = result["Image"] as? String as AnyObject? 
        properties["FacebookId"] = result["FacebookId"] as? Int as AnyObject? 
        properties["FacebookMail"] = result["FacebookMail"] as? String as AnyObject? 
        properties["Phone"] = result["Phone"] as? String as AnyObject? 
        properties["Code"] = result["Code"] as? String as AnyObject? 
        properties["Token"] = result["Token"] as? String as AnyObject? 
        properties["ServerCurrentDate"] = result["ServerCurrentDate"] as? String as AnyObject? 
        let loginget = Login(properties: properties) 
        login.append(loginget) 
       } 

      } 
     } catch { 
      print(error) 
     } 
     return login 
    } 

而且我的本地JSON文件

response.json

{ 
    "Result": { 
     "IsSuccess": true, 
     "MemberId": 73, 
     "Name": "X4r-7a", 
     "Image": "MemberImage/48a25240-26a9-46bf-8eb5-72a6b86ad555.png", 
     "FacebookId": null, 
     "FacebookMail": null, 
     "Phone": "0111111112", 
     "Code": "5425", 
     "Token": "570f4112-0f37-4e23-a2d7-5dad6af9378a", 
     "ServerCurrentDate": "2016-11-23-10-41-32" 
    } 
} 
+0

@Vinodth ty男人請批准編輯。你的代碼與真正的json文件一起工作,但我認爲我有服務器端問題。 – SwiftDeveloper

+0

你只有我給了這個迴應,所以我用它。 – Vinodh

2

你的值是一個更深一步的JSON結構:

... 

if let result = jsonDict["Result"] as? [String : AnyObject] 
{ 
    properties[LoginJsonKeys.Name] = result["Name"] as? String 
} 
... 

希望幫助

+0

是的,我將它添加到我的課程代碼中,就像你展示的一樣,但是當輸出結果相同時結果是零。 – SwiftDeveloper