본 코드는 위 책의 내용을 상당히 많이 참고했다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State createState() {
return MyAppState();
}
}
class MyAppState extends State {
String result = ''; // http req, res를 받는 string
// http request의 get을 호출
onPressGet() async {
try {
Map<String, String> headers = {
'content-type': 'application/json',
'accept': 'application/json',
};
http.Response response = await http.get(
Uri.parse('https://jsonplaceholder.typicode.com/posts/1'),
headers: headers);
print('statusCode : ${response.statusCode}');
// http 상태코드가 200 = 성공
if (response.statusCode == 200) {
setState(() {
result = response.body;
});
} else {
print("error");
}
} catch (e) {
print("error $e");
}
}
// http request의 post를 호출
onPressPost() async {
try {
http.Response response = await http.post(
Uri.parse('https://jsonplaceholder.typicode.com/posts'),
body: {'title': 'hello', 'body': 'world', 'userId': '1'});
print('statusCode : ${response.statusCode}');
// http 상태코드가 200 = 성공, 201 = 성공 뒤 작성
if (response.statusCode == 200 || response.statusCode == 201) {
setState(() {
result = response.body;
});
} else {
print("error");
}
} catch (e) {
print("error $e");
}
}
// http request의 client를 호출
onPressClient() async {
var client = http.Client();
try {
http.Response response = await client
.post(Uri.parse('https://jsonplaceholder.typicode.com/posts'), body: {
'title': '나의 일기장',
'body': '오늘은 도서관에 와서 공부 중입니다.',
'userId': '1'
});
print('statusCode : ${response.statusCode}');
// http 상태코드가 200 = 성공, 201 = 성공 뒤 작성
if (response.statusCode == 200 || response.statusCode == 201) {
response ==
await client
.get(Uri.parse('https://jsonplaceholder.typicode.com/posts'));
setState(() {
result = response.body;
});
} else {
print('error');
}
} finally {
client.close();
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Test"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("$result"),
ElevatedButton(
onPressed: onPressGet,
child: Text("Get"),
),
ElevatedButton(
onPressed: onPressPost,
child: Text("Post"),
),
ElevatedButton(onPressed: onPressClient, child: Text("Client")),
],
),
),
),
);
}
}
이 내용에서는 get, post, client 요청을 보내고 결괏값을 수신한다.
get에서는 header 내용을 정의하고 보내면 라틴어 격언으로 보이는 구절이 온다. get은 읽을 때 사용한다.
post는 말 그대로 포스트다. client도 똑같다. 차이점이란 post는 내장 http 클라이언트를 이용한다. 반대로 client는 http.Client() 객체를 이용한다.
이하는 json 파일로 정렬하는 방식이다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State createState() {
return MyAppState();
}
}
class MyAppState extends State {
String result = ''; // http req, res를 받는 string
Map<String, dynamic> responseData = {};
// http request의 get을 호출
onPressGet() async {
try {
Map<String, String> headers = {
'content-type': 'application/json',
'accept': 'application/json',
};
http.Response response = await http.get(
Uri.parse('https://jsonplaceholder.typicode.com/posts/1'),
headers: headers);
print('statusCode : ${response.statusCode}');
// http 상태코드가 200 = 성공
if (response.statusCode == 200) {
setState(() {
result = response.body;
responseData = jsonDecode(result);
});
} else {
print("error");
}
} catch (e) {
print("error $e");
}
}
// http request의 post를 호출
onPressPost() async {
try {
http.Response response = await http.post(
Uri.parse('https://jsonplaceholder.typicode.com/posts'),
body: {'title': 'hello', 'body': 'world', 'userId': '1'});
print('statusCode : ${response.statusCode}');
// http 상태코드가 200 = 성공, 201 = 성공 뒤 작성
if (response.statusCode == 200 || response.statusCode == 201) {
setState(() {
result = response.body;
responseData = jsonDecode(result);
});
} else {
print("error");
}
} catch (e) {
print("error $e");
}
}
// http request의 client를 호출
onPressClient() async {
var client = http.Client();
try {
http.Response response = await client
.post(Uri.parse('https://jsonplaceholder.typicode.com/posts'), body: {
'title': '나의 일기장',
'body': '오늘은 도서관에 와서 공부 중입니다.',
'userId': '1'
});
print('statusCode : ${response.statusCode}');
// http 상태코드가 200 = 성공, 201 = 성공 뒤 작성
if (response.statusCode == 200 || response.statusCode == 201) {
response ==
await client
.get(Uri.parse('https://jsonplaceholder.typicode.com/posts'));
setState(() {
result = response.body;
responseData = jsonDecode(result);
});
} else {
print('error');
}
} finally {
client.close();
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Test"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: onPressGet,
child: Text("Get"),
),
ElevatedButton(
onPressed: onPressPost,
child: Text("Post"),
),
ElevatedButton(onPressed: onPressClient,
child: Text("Client")
),
if (responseData.isNotEmpty)
Table(
children: [
TableRow(
children: [
Text(responseData["title"].toString()),
Text(responseData["body"].toString()),
Text(responseData["userID"].toString()),
],
)
],
) else Text("No data")
],
),
),
),
);
}
}
이렇게 테이블로 정렬할 수도 있다.
responseData라는 Map 객체를 만들고, result를 json 형식으로 변환한다. 그리고 Text로 다시 변환한다.
만약에 List로 변환한다면 리스트의 개수에 따라 유동적인 반환이 가능해진다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State createState() {
return MyAppState();
}
}
class MyAppState extends State {
String result = ''; // http req, res를 받는 string
Map<String, dynamic> responseData = {};
List responseList = [];
// http request의 get을 호출
onPressGet() async {
try {
Map<String, String> headers = {
'content-type': 'application/json',
'accept': 'application/json',
};
http.Response response = await http.get(
Uri.parse('https://jsonplaceholder.typicode.com/posts/1'),
headers: headers);
print('statusCode : ${response.statusCode}');
// http 상태코드가 200 = 성공
if (response.statusCode == 200) {
setState(() {
result = response.body;
responseData = jsonDecode(result);
responseList =
responseData.entries.map((e) => '${e.key}: ${e.value}').toList();
});
} else {
print("error");
}
} catch (e) {
print("error $e");
}
}
// http request의 post를 호출
onPressPost() async {
try {
http.Response response = await http.post(
Uri.parse('https://jsonplaceholder.typicode.com/posts'),
body: {'title': 'hello', 'body': 'world', 'userId': '1'});
print('statusCode : ${response.statusCode}');
// http 상태코드가 200 = 성공, 201 = 성공 뒤 작성
if (response.statusCode == 200 || response.statusCode == 201) {
setState(() {
result = response.body;
responseData = jsonDecode(result);
responseList =
responseData.entries.map((e) => '${e.key}: ${e.value}').toList();
});
} else {
print("error");
}
} catch (e) {
print("error $e");
}
}
// http request의 client를 호출
onPressClient() async {
var client = http.Client();
try {
http.Response response = await client
.post(Uri.parse('https://jsonplaceholder.typicode.com/posts'), body: {
'title': '나의 일기장',
'body': '오늘은 도서관에 와서 공부 중입니다.',
'userId': '1'
});
print('statusCode : ${response.statusCode}');
// http 상태코드가 200 = 성공, 201 = 성공 뒤 작성
if (response.statusCode == 200 || response.statusCode == 201) {
response ==
await client
.get(Uri.parse('https://jsonplaceholder.typicode.com/posts'));
setState(() {
result = response.body;
responseData = jsonDecode(result);
responseList =
responseData.entries.map((e) => '${e.key}: ${e.value}').toList();
});
} else {
print('error');
}
} finally {
client.close();
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Test"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: onPressGet,
child: Text("Get"),
),
ElevatedButton(
onPressed: onPressPost,
child: Text("Post"),
),
ElevatedButton(onPressed: onPressClient, child: Text("Client")),
if (responseData.isNotEmpty)
Table(
children: [
TableRow(
children: [
for (var i in responseList)
Text(i, style: TextStyle(
fontSize: 20,
)),
],
)
],
)
else
Text("No data")
],
),
),
),
);
}
}