alamofire简单使用

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值