| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,8 +2,8 @@ | |||
| 2 | 2 | // ViewController.swift | |
| 3 | 3 | // testswift | |
| 4 | 4 | // | |
| 5 | - // Created by Tommy on 17/11/28. | ||
| 6 | - // Copyright © 2017年 APIJSON. All rights reserved. | ||
| 5 | + // Created by TommyLemon on 17/11/28. | ||
| 6 | + // Copyright © 2017年 https://github.com/TommyLemon/APIJSON . All rights reserved. | ||
| 7 | 7 | // | |
| 8 | 8 | ||
| 9 | 9 | import UIKit | |
@@ -29,11 +29,26 @@ class ViewController: UIViewController { | |||
| 29 | 29 | * 通过POST请求测试APIJSON | |
| 30 | 30 | */ | |
| 31 | 31 | func test() { | |
| 32 | - | ||
| 32 | + | ||
| 33 | + let url = "http://39.108.143.172:8080/get"; | ||
| 34 | + //要发送的请求数据 | ||
| 35 | + let json = [ | ||
| 36 | + //返回数据太长 "[]": [ | ||
| 37 | + "User": [ //如果对象value是空的,请用[:]表示value,否则会被Swift解析为空数组[],而不是空对象{} | ||
| 38 | + "sex": 1 | ||
| 39 | + ] | ||
| 40 | + //] | ||
| 41 | + ] | ||
| 42 | + | ||
| 43 | + let req = toJSONString(json); | ||
| 44 | + | ||
| 45 | + print("start http request...\n\nURL = " + url + "\nRequest = \n" + req) | ||
| 46 | + | ||
| 33 | 47 | //生成UI <<<<<<<<<<<<<<<<<<<<<< | |
| 34 | - | ||
| 48 | + | ||
| 49 | + | ||
| 35 | 50 | let requestLabel = UILabel(frame:CGRect(x:20, y:10, width:400, height:130)) | |
| 36 | - requestLabel.text = "Request:\n{\n \"User\": {\n \"sex\": 1\n }\n}" | ||
| 51 | + requestLabel.text = "Request:\n" + req; | ||
| 37 | 52 | requestLabel.numberOfLines = 6 | |
| 38 | 53 | self.view.addSubview(requestLabel) | |
| 39 | 54 | ||
@@ -44,25 +59,13 @@ class ViewController: UIViewController { | |||
| 44 | 59 | ||
| 45 | 60 | //生成UI >>>>>>>>>>>>>>>>>>>>> | |
| 46 | 61 | ||
| 62 | + | ||
| 47 | 63 | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - print("start http request...\n") | ||
| 51 | - | ||
| 52 | - //要发送的请求数据 | ||
| 53 | - let json = [ | ||
| 54 | - //返回数据太长 "[]": [ | ||
| 55 | - "User": [ | ||
| 56 | - "sex": 1 | ||
| 57 | - ] | ||
| 58 | - //] | ||
| 59 | - ] | ||
| 60 | 64 | ||
| 61 | 65 | ||
| 62 | 66 | //请求URL | |
| 63 | - let url:NSURL! = NSURL(string: "http://39.108.143.172:8080/get") | ||
| 64 | 67 | ||
| 65 | - let request:NSMutableURLRequest = NSMutableURLRequest(URL: url) | ||
| 68 | + let request:NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: url)!) | ||
| 66 | 69 | ||
| 67 | 70 | request.HTTPMethod = "POST" | |
| 68 | 71 | //设置发送的数据格式为JSON | |
@@ -80,7 +83,7 @@ class ViewController: UIViewController { | |||
| 80 | 83 | //发起请求 | |
| 81 | 84 | let dataTask = session.dataTaskWithRequest(request) { (data, response, error) in | |
| 82 | 85 | ||
| 83 | - print("received result!\n\n") | ||
| 86 | + print("\n\nreceived result!\n\n") | ||
| 84 | 87 | ||
| 85 | 88 | print(data) | |
| 86 | 89 | print(response) | |
@@ -91,15 +94,14 @@ class ViewController: UIViewController { | |||
| 91 | 94 | ||
| 92 | 95 | print(jsonData) | |
| 93 | 96 | ||
| 94 | - let data : NSData! = try? NSJSONSerialization.dataWithJSONObject(jsonData, options: [NSJSONWritingOptions.PrettyPrinted]) as NSData! | ||
| 95 | - let str = String(data: data, encoding: NSUTF8StringEncoding) | ||
| 96 | - print("str = \n" + str!) | ||
| 97 | + let res:String = self.toJSONString(jsonData); | ||
| 98 | + print("Response = \n" + res) | ||
| 97 | 99 | ||
| 98 | 100 | ||
| 99 | 101 | //显示返回结果 | |
| 100 | 102 | dispatch_async(dispatch_get_main_queue(), { | |
| 101 | 103 | ||
| 102 | - responseLable.text = "Response:\n" + str! | ||
| 104 | + responseLable.text = "Response:\n" + res | ||
| 103 | 105 | print("set text end\n\n") | |
| 104 | 106 | }); | |
| 105 | 107 | ||
@@ -109,6 +111,12 @@ class ViewController: UIViewController { | |||
| 109 | 111 | dataTask.resume() | |
| 110 | 112 | } | |
| 111 | 113 | ||
| 114 | + func toJSONString(jsonData: NSDictionary!) -> String { | ||
| 115 | + let data : NSData! = try? NSJSONSerialization.dataWithJSONObject(jsonData, options: [NSJSONWritingOptions.PrettyPrinted]) as NSData! | ||
| 116 | + let str = String(data: data, encoding: NSUTF8StringEncoding) | ||
| 117 | + return str! | ||
| 118 | + } | ||
| 119 | + | ||
| 112 | 120 | ||
| 113 | 121 | } | |
| 114 | 122 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments