1 AppDelegate.swift

//
// AppDelegate.swift
// QQDemo
//
// Created by 赵超 on 14-6-21.
// Copyright (c) 2014年 赵超. All rights reserved.
// import UIKit @UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// Override point for customization after application launch.
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible() var root=RootViewController()
self.window!.rootViewController=root return true
} func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
} func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
} func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
} func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
} func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
} }

2.rootViewController.swift

//
// RootViewController.swift
// QQDemo
//
// Created by 赵超 on 14-6-21.
// Copyright (c) 2014年 赵超. All rights reserved.
// import UIKit class RootViewController: UIViewController { var btn:UIButton? var text:UITextField?
var lable:UILabel? override func viewDidLoad() {
super.viewDidLoad()
//TextField
text=UITextField()
text!.frame=CGRectMake(20,30,self.view.bounds.size.width-40,30)
text!.placeholder="http://"
text!.borderStyle = .RoundedRect //Button
btn=UIButton()
btn!.frame=CGRectMake(20, 80, 60, 30)
btn!.setTitle("HTTP",forState:.Normal)
btn!.addTarget(self,action:"onclick",forControlEvents:.TouchUpInside)
btn!.backgroundColor=UIColor.redColor() //Lable
lable=UILabel()
lable!.frame=CGRectMake(0,120,self.view.bounds.size.width,self.view.bounds.size.height)
lable!.backgroundColor=UIColor.grayColor()
lable!.lineBreakMode = .ByWordWrapping;
lable!.numberOfLines=0 self.view.addSubview(self.btn)
self.view.addSubview(lable)
self.view.addSubview(text) // Do any additional setup after loading the view.
} func connection(connection:NSURLConnection!,didReceiveData data:NSData!){
var returnString:NSString?
returnString=NSString(data:data,encoding:NSUTF8StringEncoding)
println(returnString)
let jsonData = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
var str="result:\n"+String(returnString!) for (key,value) in jsonData{
str+="\n key-->\(key)"+" value=\(value) "
}
lable!.text=str } func onclick(){
var url:String=text!.text
if url=="" {
// let alert=UIAlertView()
// alert.title="Error"
// alert.message="please enter url!"
// alert.addButtonWithTitle("Ok")
// alert.show()
var urlString:String?="http://192.168.1.104:8080/Attendace/JsonController/getJson"
httpRequest(urlString!) }else{
httpRequest(url)
} }
func httpRequest( urlString:String){
var url:NSURL?
var requrst:NSURLRequest? var conn:NSURLConnection? url=NSURL.URLWithString(urlString)
requrst=NSURLRequest(URL:url)
conn=NSURLConnection(request: requrst,delegate: self)
println(conn)
if(conn){
println("http连接成功!")
}else{
println("http连接失败!")
} } override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }


版权声明:本文博主原创文章,博客,未经同意不得转载。

swift http请求返回json数据和分析的更多相关文章

  1. 在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法

    在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法 最近在做一个小东西,使用kindeditor上传图片的时候,自己写了一个上传的方法,按照协议规则通过ajax返回json ...

  2. php的curl获取https加密协议请求返回json数据进行信息获取

    <?php header("Content-type:text/html; charset=utf-8");function getToken($url){        $ ...

  3. AJAX请求返回JSON数据动态生成html

    1:DeliveryPersonVO对象 package com.funcanteen.business.entity.delivery.vo; import java.util.List; impo ...

  4. ajax请求返回json数据弹出下载框的解决方法

    将返回的Content-Type由application/json改为text/html. 在struts2下: <action name="XXXAjax" class=& ...

  5. MOOC(2)-Django开发get、post请求,返回json数据

    1.对get请求直接返回参数 如果请求多个参数,也只能返回一个参数,这里只返回了username参数 如果想要返回多个参数值,可以返回json格式数据 2.对get请求返回json数据 # views ...

  6. 如何在Crystal Portlet中正确返回JSON数据给AJAX请求?

    当Crystal Portlet中需要采用Ajax请求,并让后台返回Json数据时,如何才能正确.方便的返回Json数据呢? 以下两种方法均可: 方法一:Ajax请求时,采用RenderURL,对应P ...

  7. Query通过Ajax向PHP服务端发送请求并返回JSON数据

    Query通过Ajax向PHP服务端发送请求并返回JSON数据 服务端PHP读取MYSQL数据,并转换成JSON数据,传递给前端Javascript,并操作JSON数据.本文将通过实例演示了jQuer ...

  8. ajax请求、servlet返回json数据

    ajax请求.servlet返回json数据 1.方式一 response.setcontenttype("text/html;charset=utf-8"); response. ...

  9. jquery的ajax异步请求接收返回json数据

    http://www.jb51.net/article/51122.htm jquery的ajax异步请求接收返回json数据方法设置简单,一个是服务器处理程序是返回json数据,另一种就是ajax发 ...

随机推荐

  1. serialVersionUID作用

    serialVersionUID适用于Java的序列化机制.简单来说,Java的序列化机制是通过判断类的serialVersionUID来验证版本一致性的.在进行反序列化时,JVM会把传来的字节流中的 ...

  2. H5实现图片优化上传

    一,HTML部分 <input type="file" accept="images/*"> <input class="url&q ...

  3. mock server相关解决方案

    前后端分离之后 前后端分离后, 大家从此进入了所谓的并行开发时代. 一旦完成前后端的(边界)分工, 大家就可以各司其职了. 前端在与后端交互时, 要想有效地提高工作效率, 后端的接口文档就是重中之重了 ...

  4. 随学随记之java的数据类型

    Java中的变量只有两种数据类型:基本数据类型(8种).引用数据类型 定义变量时内存中的状态变化:定义数据类型,开辟空间,存放数据. 8种基本数据类型的变量各占多大的内存空间,变量的取值范围 byte ...

  5. Sqlserver系列(二) 模糊查询 like

    通配符 % 匹配零个或多个字符 _ 匹配单个字符 []  指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符. [^] 不属于指定范围 ([a-f]) 或集合 ([abcdef] ...

  6. 论i++与++i

    网上看到好多人问i++与++i到底怎么理解,网友给出的答案几乎都是一样的.如下: i++:先进行计算,然后i自增1 ++i:i自增1,然后进行计算 并且课本上给出的解释跟这个也差不多,不过这样记起来既 ...

  7. MVC布局页占位符@RenderSection("bscript", false)

    @RenderSection("bscript", false) //false表示不是必须填充 填充bscript占位符  @section bscript{}

  8. 利用cookie改变背景色

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. volatile 和const 变量的使用

    一.volatile定义: 一个定义为volatile的变量是说这变量可能会被意想不到的被改变,这样,有了volatile变量后,就提醒编译器就不会去假设这个变量的值了.精确地说就是,编译中的优化器在 ...

  10. java的clone

    做项目时有时可能会遇到需要克隆对象的时候,因为有时候对象是直接从别的类get到的,那样引用的是一个对象,修改的话会将原先的对象也修改了. java的浅克隆,十分简单.但是只会克隆基本的数据类型,当涉及 ...