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发 ...
随机推荐
- fourinone分布式缓存研究和Redis分布式缓存研究
最近在写一个天气数据推送的项目,准备用缓存来存储数据.下面分别介绍一下fourinone分布式缓存和Redis分布式缓存,然后对二者进行对比,以供大家参考. 1 fourinone分布式缓存特性 1 ...
- 给Visual Studio更替皮肤和背景图
给Visual Studio更换皮肤和背景图 1.先安装更换皮肤的插件 VS菜单栏里面找到:工具>扩展和更新>联机>搜索: Theme Editor 下载并安装: 安装后先不着 ...
- crm操作知识库文章实体
using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using Microsoft ...
- cocos2d-x结合cocosbuilder,不同屏幕适配小结
这个问题搞了好几天才解决,在此总结一下: 首先约定只使用一套图片资源同时应用于iphon4和iphon5(测试过在ipad下也能显示正常), 这里我们将需要全屏显示的背景制作为iphon5的尺寸即:1 ...
- android——字体颜色跟随状态改变
TextView的字体颜色也可以和ImageView的background一样,跟随状态发生改变.只需要自定义一下字体颜色.在color文件夹下面,新建一个颜色文件的xml. OK ,这就完成 了. ...
- MySql高效分页SQL
public string GetQuerySql(ITSPAreaQueryModel model, object state = null) { ); sqlBuilder.AppendForma ...
- Path Sum,Path Sum II
Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a ...
- Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java IO之序列化
序列化机制是Java语言内建的一种对象持久化方式,可以很容易的在JVM中的活动对象和字节数组之间转换.它的一个重要用途就是远程方法调用的时候,用来对开发人员屏蔽底层实现细节(远端的开发人员不知道这个对 ...
- mysql查询优化技巧
索引优化,查询优化,查询缓存,服务器设置优化,操作系统和硬件优化,应用层面优化(web服务器,缓存)等等.这里记录的优化技巧更适合开发人员,都是从网络上搜集和整理的,主要是查询语句上的优化,其他层面上 ...