ios - How can I do an HTTP Post before reading the JSON? -
i reading json url , has been working correctly. code:
@iboutlet weak var profilescell: uitableview! let cellspacing: cgfloat = 50 var names = [string]() var posts = [string]() var locations = [string]() var votes = [string]() var comments = [string]() override func viewdidload() { super.viewdidload() profilescell.datasource = self let url:url = url(string: "http://"+connection_string+":8000/profile_view")! urlsession.shared.datatask(with:url, completionhandler: {(data, response, error) in if error != nil { print(error) } else { { let parseddata = try jsonserialization.jsonobject(with: data!, options: .allowfragments) as! [string:any] if let profile = parseddata["profile"] as! [anyobject]? { stream in profile { if let fullname = stream["fullname"] as? string { self.names.append(fullname) } if let post = stream["post"] as? string { self.posts.append(post) } if let location = stream["location"] as? string { self.locations.append(location) } if let vote = stream["votes"] as? string { self.votes.append(vote.appending(" votes")) } if let comment = stream["comments"] as? string { self.comments.append(comment.appending(" comments")) } dispatchqueue.main.async { self.profilescell.reloaddata() } } } } catch let error nserror { print(error) } } }).resume() }
that code above correctly parses json , data returned tableview. want http post before reading json , parameter name profile_id
, know wrong in code because if html form parameter, things work correctly.
this new code have:
@iboutlet weak var profilescell: uitableview! let cellspacing: cgfloat = 50 var names = [string]() var posts = [string]() var locations = [string]() var votes = [string]() var comments = [string]() override func viewdidload() { super.viewdidload() profilescell.datasource = self let url:url = url(string: "http://"+connection_string+":8000/profile_view")! let ss = "32" var request = urlrequest(url:url) let paramstring = "profile_id=\(ss)" request.httpmethod = "post" request.cachepolicy = nsurlrequest.cachepolicy.reloadignoringcachedata request.httpbody = paramstring.data(using: .utf8) urlsession.shared.datatask(with:url, completionhandler: {(data, response, error) in if error != nil { print(error) } else { { let parseddata = try jsonserialization.jsonobject(with: data!, options: .allowfragments) as! [string:any] if let profile = parseddata["profile"] as! [anyobject]? { stream in profile { if let fullname = stream["fullname"] as? string { self.names.append(fullname) } if let post = stream["post"] as? string { self.posts.append(post) } if let location = stream["location"] as? string { self.locations.append(location) } if let vote = stream["votes"] as? string { self.votes.append(vote.appending(" votes")) } if let comment = stream["comments"] as? string { self.comments.append(comment.appending(" comments")) } dispatchqueue.main.async { self.profilescell.reloaddata() } } } } catch let error nserror { print(error) } } }).resume() }
now code url still being hit profile_id
showing null though have hardcoded number 32. message displayed:
error domain=nscocoaerrordomain code=3840 "no value." userinfo={nsdebugdescription=no value.}
Comments
Post a Comment