我需要从web api下载所有产品。我正在使用其他api,它们运行良好。但是,当我尝试下载产品列表时,它不起作用。我在浏览器中检查它,在它正在请求的浏览器中但是当我尝试从Xcode(Swift)请求它时它不起作用。我使用了所有版本来请求:Alamofire,尝试使用NSURLSession
和NSURLConnection
,但他们都没有工作。我该怎么做呢?
这是我的工作要求:
func getTovar(){
data.removeAll()
Alamofire.request(.GET, "http://admin.unimax.kz/api/Default1/Gettovar")
.responseJSON { response in
guard response.result.error == nil else {
// got an error in getting the data, need to handle it
print("error calling GET on /todos/1")
print(response.result.error!)
return
}
if let value = response.result.value {
// handle the results as JSON, without a bunch of nested if loops
let product = JSON(value)
for (_,subJson):(String, JSON) in product {
let img:NSData
if let src=subJson["sphoto"].string{
if src.containsString("jpg"){
let range = src.startIndex.advancedBy(2)..<src.endIndex
let substring = src[range]
var urlString = "http://admin.unimax.kz/\(substring)"
urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
img=NSData(contentsOfURL: NSURL(string: urlString)!)!
}
else{
img=NSData(contentsOfURL: NSURL(string: "http://zhaksy-adam.kz/Images/domalak.png")!)!
}
}
else{
img=NSData(contentsOfURL: NSURL(string: "http://zhaksy-adam.kz/Images/domalak.png")!)!
}
//Do something you want
let p=Product(id:subJson["id"].int!,title: subJson["name"].string!, img: UIImage(data: img), price: subJson["price"].int!,desc:subJson["description"].string!)
self.data.append(p)
}
}
}
}