One of the very useful functionality and frequent used for mobile application development, if application interact with server.
Most of the application communicate with server over HTTP Post request.
In this blog you can see how to communicate with server over HTTP Post method.
func postRequestToServer(postUrl:NSString, postParameters:NSDictionary) {
var url = NSURL(string: postUrl)
var postData:NSData!
postData = NSJSONSerialization.dataWithJSONObject(postParameters, options: NSJSONWritingOptions.PrettyPrinted, error: nil)
var request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: 50)
var postDataLength = NSString(format: "%lu", postData.length)
request.setValue(postDataLength, forHTTPHeaderField: "Content-Length")
request.HTTPBody = postData
request.HTTPMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
var urlResponse : NSURLResponse?
var error : NSError?
var responseData : NSData!
responseData = NSURLConnection.sendSynchronousRequest(request, returningResponse: &urlResponse, error: &error)
var returnDict:NSDictionary? = nil
if responseData==nil{
println("Nil Response")
return
}
var parseResponse: AnyObject? = NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.MutableLeaves, error: nil)
if (parseResponse?.isKindOfClass(NSDictionary) != nil){
NSLog("parse response = %@", parseResponse as NSDictionary)
}
else if ((parseResponse?.isKindOfClass(NSArray)) != nil){
NSLog("parse response = %@", parseResponse as NSArray)
}
}//End method
Most of the application communicate with server over HTTP Post request.
In this blog you can see how to communicate with server over HTTP Post method.
func postRequestToServer(postUrl:NSString, postParameters:NSDictionary) {
var url = NSURL(string: postUrl)
var postData:NSData!
postData = NSJSONSerialization.dataWithJSONObject(postParameters, options: NSJSONWritingOptions.PrettyPrinted, error: nil)
var request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: 50)
var postDataLength = NSString(format: "%lu", postData.length)
request.setValue(postDataLength, forHTTPHeaderField: "Content-Length")
request.HTTPBody = postData
request.HTTPMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
var urlResponse : NSURLResponse?
var error : NSError?
var responseData : NSData!
responseData = NSURLConnection.sendSynchronousRequest(request, returningResponse: &urlResponse, error: &error)
var returnDict:NSDictionary? = nil
if responseData==nil{
println("Nil Response")
return
}
var parseResponse: AnyObject? = NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.MutableLeaves, error: nil)
if (parseResponse?.isKindOfClass(NSDictionary) != nil){
NSLog("parse response = %@", parseResponse as NSDictionary)
}
else if ((parseResponse?.isKindOfClass(NSArray)) != nil){
NSLog("parse response = %@", parseResponse as NSArray)
}
}//End method
No comments:
Post a Comment