我有一个Swift应用程序,它将时间戳和数字作为json文件加载,并通过Alamofire发送它。
var newPostStamp = ["title": "\(stamp)", "body": string1]
Alamofire.request(.POST, "mywebsite.com/post", parameters: newPostStamp, encoding: .JSON)
.responseJSON { (request, response, data, error)}...
你能告诉我如何在服务器的POST请求中处理参数吗?我正在使用本教程(http://joelinoff.com/blog/?p=1658),但我不知道如何更改do_POST()方法来处理我的请求。我只需要它向我展示一个包含所有时间戳的新页面:到目前为止我发送的值。
谢谢!
答案 0 :(得分:0)
这对我有用。
// Add this to your class
import SystemConfiguration
import CoreData
func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress){
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)).takeRetainedValue()
}
var flags: SCNetworkReachabilityFlags = 0
if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == 0 {
return false
}
let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return (isReachable && !needsConnection) ? true : false
}
func connectionService(jsonString:NSDictionary) -> NSDictionary
{
var request = NSMutableURLRequest(URL: NSURL(string: "https://example.com")!)
var response: NSURLResponse?
var error: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(jsonString, options: nil, error: &error)
request.timeoutInterval = 100
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("gzip", forHTTPHeaderField: "Accept-encoding")
//Reconnnection 5 times
for (var i = 0; i < 5 ; i++)
{
let date1 = NSDate();
var formatter = NSDateFormatter();
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS";
let defaultTimeZoneStr1 = formatter.stringFromDate(date1);
var error: NSError?
println("Connecting to server : \(defaultTimeZoneStr1)")
if isConnectedToNetwork (){
let urlData = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)
if error != nil || urlData!.length == 0
{
println("Error happend timeout======\(error?.code)!")
continue
}
else
{
let date = NSDate();
var formatter = NSDateFormatter();
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS";
let defaultTimeZoneStr = formatter.stringFromDate(date);
println("Response from server : \(defaultTimeZoneStr)")
if let httpResponse = response as? NSHTTPURLResponse
{
println("Status Code for successful------------------------------------>\(httpResponse.statusCode)")
if (httpResponse.statusCode == 502)
{
// Server is down
break
}
else
{
println(NSString(data: urlData!, encoding: NSUTF8StringEncoding)!)
var er: NSError?
let JSONresdata: AnyObject = (NSJSONSerialization.JSONObjectWithData(urlData!, options: .MutableContainers,error: &er)!)
println("JSON response : \(JSONresdata)")
JSONdata = JSONresdata as! NSDictionary
}
}
}
}
else
{
println("------------------------------------------------No network----------------------------------------------")
// You can add your code
break
}
}
return JSONdata as! NSDictionary;
}
希望这可能会有所帮助。