![](/img/trans.png)
[英]How to check 3G,4G and wifi internet connection in swift 2.2
[英]Different results by parsing JSON API to WIFI and 4G in SWIFT
我有一個提供此JSON的Wordpress JSON API(這是帖子的評論列表):
JSON數據還可以,我對此帖子確實有3條評論。 在我檢查過的wordpress管理員中,所有這些評論均被批准。
我試圖讀取此JSON,以創建NSObject Comment()列表,以將其顯示在表格視圖中。
我測試了以下代碼:
let commentURL:NSURL = NSURL(string: "http://mywebsite.com/wp_api/v1/posts/1855/comments")!
if let data:NSData = NSData(contentsOfURL: commentURL) {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as! [String: AnyObject]
if let allComments = json["comments"] as? NSArray{
print(allComments)
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
}
結果很奇怪:
我已經在不同的設備和網絡上重復了測試過程。 3G / 4G網絡經常錯過最后的評論。 但可與WiFi完美搭配。
為什么?
編輯
我改進了代碼,但是仍然無法正常工作。
func downloadCommentsFrom(post:Post, completion: ((comments: [Comment]?) -> Void)) {
guard
let url = NSURL(string: "http://mywebsite.com/wp_api/v1/posts/\(post.postId)/comments")
else {return}
let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) in
completion(comments: self.getCommentsFromJSONAPI(data:data!))
}
task.resume()
}
func getCommentsFromJSONAPI(data data:NSData) -> [Comment] {
var comments:[Comment]=[]
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
if let allComments = json["comments"] as? NSArray{
comments = allComments
print(allComments)
}
} catch let error as NSError {
print("Failed to load JSON COMMENTS: \(error.localizedDescription)")
}
return comments
}
編輯2
WIFI中的HTTP響應 :
Optional(<NSHTTPURLResponse: 0x17db0a80> { URL: http://mywebsite.com/wp_api/v1/posts/1136/comments } { status code: 200, headers {
"Cache-Control" = "max-age=3600, public";
Connection = "Keep-Alive";
"Content-Encoding" = gzip;
"Content-Type" = "text/html";
Date = "Sun, 20 Dec 2015 09:42:39 GMT";
Etag = 4652938bddc2501278c4e584c4612de4;
Expires = "Sun, 20 Dec 2015 10:42:39 GMT";
"Keep-Alive" = "timeout=5, max=99";
"Last-Modified" = "Thu, 01 Jan 1970 00:00:00 GMT";
Pragma = public;
Server = Apache;
"Set-Cookie" = "300gp=R3395909593; path=/; expires=Sun, 20-Dec-2015 10:48:14 GMT";
"Transfer-Encoding" = Identity;
Vary = "Accept-Encoding,User-Agent";
"X-Powered-By" = "PHP/5.4.45";
} })
4G中的HTTP響應 :
Optional(<NSHTTPURLResponse: 0x16d0d4b0> { URL: http://cestunmac.com/wp_api/v1/posts/1136/comments } { status code: 200, headers {
Age = 208;
"Cache-Control" = "public, max-age=3600";
"Content-Encoding" = gzip;
"Content-Type" = "text/html";
Date = "Sun, 20 Dec 2015 09:40:00 GMT";
Etag = 4652938bddc2501278c4e584c4612de4;
Expires = "Sun, 20 Dec 2015 10:40:00 GMT";
"Last-Modified" = "Thu, 01 Jan 1970 00:00:00 GMT";
Pragma = public;
Server = Apache;
"Transfer-Encoding" = Identity;
Vary = "Accept-Encoding,User-Agent";
"X-Powered-By" = "PHP/5.4.45";
} })
編輯3
這似乎是因為緩存,而不是4G問題。 我在Playground(帶有WIFI網絡)中測試了我的代碼,但最后的注釋也丟失了。 我嘗試添加:
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.requestCachePolicy = .ReloadIgnoringLocalAndRemoteCacheData
let urlSession:NSURLSession = NSURLSession(configuration: configuration)
要么:
let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()()
let urlSession:NSURLSession = NSURLSession(configuration: configuration)
相同的結果,在我的Web瀏覽器中,JSON可以,但是在應用程序上不可以。 數據尚未更新。
let session = NSURLSession.sharedSession().dataTaskWithURL(commentURL!) { data, response, error in
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
if let allComments = json["comments"] as? [NSDictionary] {
print(allComments)
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
}
session.resume()
那是一個緩存服務器的問題。
編輯
我的Wordpress使用的是W3 Total Cache插件。 我不得不刪除它以避免我的問題。 我找不到確切的原因,但是現在可以正常工作了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.