1. alamofire简单文件网络请求,并处理。
// ViewController.swift
// alamofire测试
//
// Created by amesty on 15/5/30.
// Copyright (c) 2015年 cf. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//通常情况下,您只需将请求对象链接到响应方法上.
//responseJSON()方法。当网络请求完毕后,responseJSON()方法会调用我们所提供的闭包。在我们的示例中,我们只是简单的将经过解析的 JSON 输出到控制台中。
//· 调用responseJSON方法意味着您期望获得一个 JSON 数据。在我们的示例中,Alamofire 试图解析响应数据并返回一个 JSON 对象。或者,您可以使用responsePropertyList来请求获得一个属性列表,也可以使用responseString来请求获得一个初始字符串。在本教程后面,您将了解更多关于响应序列化方法的使用方式。
request(.GET,"http://localhost/demo.json").responseJSON() { (_, _, JSON, _) -> Void in
println(JSON)
}
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func demo1(){
// · Alamofire.request(_:_)接收两个参数:method和URLString。其中,method 通常是.GET、.POST;URLString通常是您想要访问的内容的 URL。其将返回一个Alamofire.Request对象。
request(.GET, "http://ww.baidu.com", parameters: nil, encoding: .URL).responseString(encoding: NSUTF8StringEncoding) { (_, _, html, error) -> Void in
if error != nil {
println(error)
return
}
println(html)
}
}
}
转载于:https://my.oschina.net/u/150265/blog/422640