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发 ...
随机推荐
- HTML5 Canvas Cheat Sheet
HTML5 Canvas Cheat Sheet HTML5 Canvas Cheat Sheet v1.x
- sendto() 向广播地址发包返回errno 13, Permission denied错误
http://blog.csdn.net/guanghua2_0beta/article/details/52483916 sendto() 向广播地址发包返回errno 13, Permission ...
- 精讲N皇后问题
思想:存三个数组记录记录走的过程,运用回溯不符合或row==n+1就跳出当前层,直到找完:递归时的路径都在保存着,当连续跳出到第一次进入的dfs且i=n时就全部跳出dfs函数了: # ...
- Swift中NSData与NSDictionary之间的相互转换
原创Blog,转载请注明出处 使用NSKeyedUnarchiver类来进行相互转换 1.NSDictionary转NSData var dictionaryExample : [String:Any ...
- 【剑指offer】Q40:数组中出现一次的数
书里面关于分类的推断有些麻烦,通过某一位为0为1来对数组元素进行分类.假如第3位为1.那么也就是元素x & 8 等于或不等于0,所以不是必需非的用第几位去推断. def once(array) ...
- RHEL5.8安装Sybase 15.7_x86_64
RHEL5.8安装Sybase 15.7如果您运行的是 RHEL 5 或更高版本,请使用以下设置:kernel.exec-shield = 0kernel.randomize_va_space = 0 ...
- Node.js 入门教程和学习资源汇总
这篇文章与大家分享一批很有用的 Node.js 入门教程和学习资源.Node 是一个服务器端的 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用 ...
- MySQL Update 使用
备忘: USE `xxx`; ; UPDATE `TB_MB_1` T SET T.`MedicalCount` = ( SELECT S.Total-- ,S.`HospitalID` FROM( ...
- 使用SqlBulkCopy批量插入数据
static void Main(string[] args) { //定义与目标表结构相同的DataTable DataTable dataTable = new DataTable(); data ...
- 分享一个自己写的基于JQuery的一个Web背景切换的Demo
这个效果主要有两个特点: 1. 背景切换的渐变 2. 背景大小自适应 3. 背景自适应保持比例同时, 相对居中 js源码: (function ($) { $.fn.bgChange = functi ...