swift http请求返回json数据和分析
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数据和分析的更多相关文章
- 在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法
在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法 最近在做一个小东西,使用kindeditor上传图片的时候,自己写了一个上传的方法,按照协议规则通过ajax返回json ...
- php的curl获取https加密协议请求返回json数据进行信息获取
<?php header("Content-type:text/html; charset=utf-8");function getToken($url){ $ ...
- AJAX请求返回JSON数据动态生成html
1:DeliveryPersonVO对象 package com.funcanteen.business.entity.delivery.vo; import java.util.List; impo ...
- ajax请求返回json数据弹出下载框的解决方法
将返回的Content-Type由application/json改为text/html. 在struts2下: <action name="XXXAjax" class=& ...
- MOOC(2)-Django开发get、post请求,返回json数据
1.对get请求直接返回参数 如果请求多个参数,也只能返回一个参数,这里只返回了username参数 如果想要返回多个参数值,可以返回json格式数据 2.对get请求返回json数据 # views ...
- 如何在Crystal Portlet中正确返回JSON数据给AJAX请求?
当Crystal Portlet中需要采用Ajax请求,并让后台返回Json数据时,如何才能正确.方便的返回Json数据呢? 以下两种方法均可: 方法一:Ajax请求时,采用RenderURL,对应P ...
- Query通过Ajax向PHP服务端发送请求并返回JSON数据
Query通过Ajax向PHP服务端发送请求并返回JSON数据 服务端PHP读取MYSQL数据,并转换成JSON数据,传递给前端Javascript,并操作JSON数据.本文将通过实例演示了jQuer ...
- ajax请求、servlet返回json数据
ajax请求.servlet返回json数据 1.方式一 response.setcontenttype("text/html;charset=utf-8"); response. ...
- jquery的ajax异步请求接收返回json数据
http://www.jb51.net/article/51122.htm jquery的ajax异步请求接收返回json数据方法设置简单,一个是服务器处理程序是返回json数据,另一种就是ajax发 ...
随机推荐
- Java如何访问Axis2服务端
import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.address ...
- 【Android开发经验】LayoutInflater—— 你可能对它并不了解甚至错误使用
今天,看到了一篇文章讲LayoutInflater的使用方法.瞬间感觉自己对这个类确实不够了解,于是简单的看了下LayoutInflater类的源码.对这个类有了新的认识. 首先.LayoutInfl ...
- 用Jfree实现条形柱状图表,java代码实现
用Jfree实现条形柱状图表,java代码实现.可经经常使用于报表的制作,代码自己主动生成后能够自由查看.能够自由配置图表的各个属性,用来达到自己的要求和目的 package test1; impor ...
- JS实现给页面表单设置触发默认按钮
var defaultBtnId; function setDefaultButton(id) { defaultBtnId = id; } document.onkeydown = function ...
- javascript模式——Flyweight
Flyweight是一种共享数据内存的模式. Flyweight模式是将一些公有属性从个人中剔除,放在共享的对象当中. 下面以一个项目实例,通过不断的改进,以显示Flyweight模式的优点. 现在我 ...
- ExtJs4.0入门错误
当在eclipse中的web工程中增加了extjs4,出现An internal error occurred during: "Building workspace". Java ...
- 判断网络是否连接Internet
添加 system32.Management 引用 private bool ListenNET() { ManagementObjectSearcher s = ...
- ( function(){…} )()
javascript和其他编程语言相比比较随意,所以javascript代码中充满各种奇葩的写法,有时雾里看花,当然,能理解各型各色的写法也是对javascript语言特性更进一步的深入理解. ( f ...
- asp.net实现伪静态遇到的问题
之前在一次项目(asp.net网站)中要用到伪静态技术,实现的思路大致是这样的: 在全局配置文件Global.asax(普通的类可以通过实现IHttpModule来完成)中的Application_B ...
- js如何控制css伪元素内容(before,after)
曾经遇到的问题,在对抗UC浏览器屏蔽需要把内容输出到css 伪元素中输出.有个疑问如何用js控制它.于是在segmentfault提问,如下是对问题的整理: 如何用js控制css伪类after 简单粗 ...