我编写了以下请求代码(使用SwiftHTTP库)
func performTagRequest(detail : String){
var request = HTTPTask()
var formattedDetail = detail.stringByReplacingOccurrencesOfString("\"", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
var url = "www.gifbase.com/tag/\(formattedDetail)?format=json"
url = url.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
println(url)
request.GET(url, parameters: nil, success: {(response: HTTPResponse) in
if response.responseObject != nil {
let data = response.responseObject as NSData
let str = NSString(data: data, encoding: NSUTF8StringEncoding)!
let gifMetaArray = str.componentsSeparatedByString(",") as [String]
//println("response: \(gifMetaArray))") //prints the HTML of the page
}
},failure: {(error: (NSError, HTTPResponse?)) in
println("error: \(error)")
})
}
在浏览器中返回有效响应(ex for "10thingsihateaboutyou")但返回错误
error: (Error Domain=NSURLErrorDomain Code=-1002 "The operation couldn’t be completed.
(NSURLErrorDomain error -1002.)" UserInfo=0x7ff22947b640
{NSErrorFailingURLStringKey=www.gifbase.com/tag/10thingsihateaboutyou?format=json,
NSUnderlyingError=0x7ff22940d550 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork
error -1002.)", NSErrorFailingURLKey=www.gifbase.com/tag/10thingsihateaboutyou?format=json}, nil)
我发现错误-1002是NSURLErrorUnsupportedURL
,可能是由于没有正确转义字符引起的,但是我打印的行应该是我认为的。
任何人都有这方面的经验吗?
答案 0 :(得分:3)
我通过摆弄更多来找到答案。我忘记了“http://”,因为我被桌面浏览器叮当作响......