Skip to content

Commit 9031b32

Browse files
committed
优化log
1 parent 1f9baed commit 9031b32

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

APIJSON-iOS/APIJSON-Swift/APIJSONTest/ViewController.swift

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// ViewController.swift
33
// testswift
44
//
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.
77
//
88

99
import UIKit
@@ -29,11 +29,26 @@ class ViewController: UIViewController {
2929
* 通过POST请求测试APIJSON
3030
*/
3131
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+
3347
//生成UI <<<<<<<<<<<<<<<<<<<<<<
34-
48+
49+
3550
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;
3752
requestLabel.numberOfLines = 6
3853
self.view.addSubview(requestLabel)
3954

@@ -44,25 +59,13 @@ class ViewController: UIViewController {
4459

4560
//生成UI >>>>>>>>>>>>>>>>>>>>>
4661

62+
4763

48-
49-
50-
print("start http request...\n")
51-
52-
//要发送的请求数据
53-
let json = [
54-
//返回数据太长 "[]": [
55-
"User": [
56-
"sex": 1
57-
]
58-
//]
59-
]
6064

6165

6266
//请求URL
63-
let url:NSURL! = NSURL(string: "http://39.108.143.172:8080/get")
6467

65-
let request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
68+
let request:NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: url)!)
6669

6770
request.HTTPMethod = "POST"
6871
//设置发送的数据格式为JSON
@@ -80,7 +83,7 @@ class ViewController: UIViewController {
8083
//发起请求
8184
let dataTask = session.dataTaskWithRequest(request) { (data, response, error) in
8285

83-
print("received result!\n\n")
86+
print("\n\nreceived result!\n\n")
8487

8588
print(data)
8689
print(response)
@@ -91,15 +94,14 @@ class ViewController: UIViewController {
9194

9295
print(jsonData)
9396

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)
9799

98100

99101
//显示返回结果
100102
dispatch_async(dispatch_get_main_queue(), {
101103

102-
responseLable.text = "Response:\n" + str!
104+
responseLable.text = "Response:\n" + res
103105
print("set text end\n\n")
104106
});
105107

@@ -109,6 +111,12 @@ class ViewController: UIViewController {
109111
dataTask.resume()
110112
}
111113

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+
112120

113121
}
114122

0 commit comments

Comments
 (0)