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发 ...
随机推荐
- python3 时间和日期
Python程序可以通过多种方式来处理日期和时间.日期格式之间的转换是计算机的一个共同核心.Python的时间和日历模块能够帮助我们跟踪的日期和时间. 什么是刻度? 时间间隔以秒为单位的浮点数.特别是 ...
- 只允许指定IP访问指定端口 ufw
那天工作需要在服务器上指定ip才可以访问指定端口,配置命令如下: (服务器是ubuntu 14.04系统) apt-get install ufw ufw enable ufw default den ...
- 修改linux共享内存大小
这是实际linux系统显示的实际数据: beijibing@bjb-desktop:/proc/sys/kernel$ cat shmmax 33554432 beijibing@bjb-deskt ...
- Zero Downtime Upgrade of Oracle 10g to Oracle 11g Using GoldenGate — 2
Prepare 10g Database for OGG Create GGS and GGS_MON Database Users SQL> create tablespace ggs_tbs ...
- Android项目实战-云词典
前段时间在网上看到滴答滴答的一个项目,字典文章,找到一个简单的字.整句翻译功能,词汇,但漫长的岁月项目,二手API数据不再可用,我决定使用其现有的框架来实现其功能,主要采用的技术GSON和Volley ...
- 多点触控插件Hammer.js
插件描述:Hammer.js是一个开源的,轻量级的javascript库,它可以在不需要依赖其他东西的情况下识别触摸,鼠标事件. 使用方法: <script src=<span class ...
- sql必知必会(第四版) 学习笔记二 视图
本书用到的几个表的建表sql语句如下: --销售产品供应商 CREATE TABLE Vendors ( vend_id varchar(20) not null, vend_name varchar ...
- K - Digital Roots(第二季水)
Description The digital root of a positive integer is found by summing the digits of the integer. If ...
- LinearLayout遇到的问题——利用LinearLayout做横向滑动冲突
问题:当我添加两个TextView的时候,然后滑动,发现只生成了一个TextView. 就是 <?xml version="1.0" encoding="utf-8 ...
- ASCII码排序,hdu-2000
Problem Description 输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符. Input 输入数据有多组,每组占一行,有三个字符组成,之间无空格. Output ...