Swift HTTP request returns JSON data and analysis

Source: Internet
Author: User

1 Appdelegate.swift
appdelegate.swift//qqdemo////Created by Carrie on 14-6-21.//Copyright (c) 2014 Carrie.                                All rights Reserved.//import Uikit@uiapplicationmainclass 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 Applicatio        N Launch. self.window!. BackgroundColor = Uicolor.whitecolor () self.window!. Makekeyandvisible () var root=rootviewcontroller () self.window!. Rootviewcontroller=root return True} func applicationwillresignactive (application:u IApplication) {//Sent when the application are about-to-move from active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS MessaGE) or when the user quits the application and it begins the transition to the background state. Use the This method to the 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 resource s, save user data, invalidate timers, and store enough application state information to restore your application to its CU        Rrent 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's can undo many of the changes made on entering the background. } func applicationdidbecomeactive (application:uiapplication) {//RestaRT any tasks this were paused (or not yet started) while the application is inactive.    If the application is previously in the background, optionally refresh the user interface. } func applicationwillterminate (application:uiapplication) {//Called when the application are about to Termina Te. Save data if appropriate.    See also Applicationdidenterbackground:. }}

2.rootviewcontroller.swift
rootviewcontroller.swift//qqdemo////Created by Carrie on 14-6-21.//Copyright (c) 2014 Carrie. All rights Reserved.//import Uikitclass 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 (a), 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.addSubvi EW (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 = NSJSONSe Rialization. Jsonobjectwithdata (data, Options:NSJSONReadingOptions.MutableContainers, Error:nil) as nsdictionary var str= "ResU lt:\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 connection succeeded!") }else{println ("HTTP connection failed!") }} override Func Didreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any Resou RCEs that can is recreated. } }



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Swift HTTP request returns JSON data and analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.