Reputation: 7366
I have post request and parse response json codes but my codes dont working.
My class codes
@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
}
And when i click to action button that codes gives me only
nil
output. Where i doing mistake any idea ?
Upvotes: 0
Views: 1079
Reputation: 5268
Please find the below code am using which is working fine with local son.Since you haven't provided the url and other details
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
}
And my local json file
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"
}
}
Upvotes: 1
Reputation: 1003
Your values are one step deeper in the json structure:
...
if let result = jsonDict["Result"] as? [String : AnyObject]
{
properties[LoginJsonKeys.Name] = result["Name"] as? String
}
...
hope that helps
Upvotes: 2