ios - making nsurlsession request with parameters -


hy, new swift , doing work on nsurlsession. here code

  func webrequesttoserver() {             let request = nsurlrequest(url: nsurl(string:"some url")!)             let urlsession = nsurlsession.sharedsession()             let task = urlsession.datataskwithrequest(request, completionhandler: {                 (data, response, error) -> void in                 if let error = error {                     print(error)                     return }                 // parse json data                 if let data = data {                      let datastring = nsstring(data: data, encoding: nsutf8stringencoding)                     print(datastring)                     nsoperationqueue.mainqueue().addoperationwithblock({ () -> void in                      })                 } })              task.resume()         } 

thus code data arrives websrvice want first takes 2 parameters me , show data? please me. sorry english. it's weak.

frame parameters dictionary format , set parameters httpbody.below code helps you...

let myurl = nsurl(string: "http://o16-labs.com/swapby/getcategoryinfo");     let request = nsmutableurlrequest(url:myurl!);     request.httpmethod = "post";     // compose query string     let poststring = "firstname=james&lastname=bond";      request.httpbody = poststring.datausingencoding(nsutf8stringencoding);      let task = nsurlsession.sharedsession().datataskwithrequest(request) {         data, response, error in          if error != nil         {             print("error=\(error)")             return         }          // can print out response object         print("response = \(response)")          // print out response body         let responsestring = nsstring(data: data!, encoding: nsutf8stringencoding)         print("responsestring = \(responsestring)")          //let’s convert response sent server side script nsdictionary object:          {             if let jsonresult = try nsjsonserialization.jsonobjectwithdata(data!, options: []) as? nsdictionary {                 print(jsonresult)             }         } catch let error nserror {             print(error.localizeddescription)         }      }      task.resume() 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -