質問編集履歴

3

内容追加

2018/07/10 02:45

投稿

wangzj
wangzj

スコア53

test CHANGED
File without changes
test CHANGED
@@ -12,9 +12,289 @@
12
12
 
13
13
  ResponseはJsonデータなので、Jsonデータ処理(整形、Mapに転換など)が必要です。
14
14
 
15
- 実際のコードなら、前の検証コードに関する質問をご参照すればと思ます:
15
+ 実際のコードなら、をご参照くださ
16
+
16
-
17
+ ※作成中ですが、json処理を入れていない。json処理を入れると他のjarをIMPORTする必要がある
18
+
19
+ ```java
20
+
21
+ import java.io.BufferedReader;
22
+
23
+ import java.io.DataOutputStream;
24
+
25
+ import java.io.IOException;
26
+
27
+ import java.io.InputStreamReader;
28
+
29
+ import java.io.StringWriter;
30
+
31
+ import java.net.HttpURLConnection;
32
+
33
+ import java.net.MalformedURLException;
34
+
35
+ import java.net.URL;
36
+
37
+
38
+
39
+ public class Main {
40
+
41
+
42
+
43
+ private static String urlBase = "http://127.0.0.1:8080";
44
+
45
+ private static String urlOperate = "http://127.0.0.1:8081";
46
+
47
+
48
+
49
+ public static void main(String[] args) {
50
+
51
+
52
+
53
+ try {
54
+
55
+ if (args == null || args.length == 0) {
56
+
57
+ getLatest();
58
+
59
+ } else {
60
+
61
+ switch (args[0]) {
62
+
63
+ case "latest":
64
+
65
+ getLatest();
66
+
67
+ break;
68
+
69
+ case "address":
70
+
71
+ getNewAddress();
72
+
73
+ break;
74
+
75
+ case "txes":
76
+
77
+ getTxes(args[1]);
78
+
79
+ break;
80
+
81
+ case "getTokensByAddress":
82
+
83
+ getAllTokensByAddress(args[1]);
84
+
85
+ break;
86
+
87
+ default:
88
+
89
+ System.out.println("入力ミスです。");
90
+
91
+ }
92
+
93
+ }
94
+
95
+
96
+
97
+ } catch (MalformedURLException e) {
98
+
99
+ e.printStackTrace();
100
+
101
+ } catch (IOException e) {
102
+
103
+ e.printStackTrace();
104
+
105
+ }
106
+
107
+ }
108
+
109
+
110
+
111
+ public static void getLatest() {
112
+
113
+ try {
114
+
115
+ URL url = new URL(urlOperate + "/blockchain/latest");
116
+
117
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
118
+
119
+ System.out.printf("Response: %d %s\n", conn.getResponseCode(), conn.getResponseMessage());
120
+
121
+ BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
122
+
123
+ String input;
124
+
125
+
126
+
127
+ while ((input = br.readLine()) != null) {
128
+
129
+ System.out.println(input);
130
+
131
+ }
132
+
133
+ br.close();
134
+
135
+
136
+
137
+ } catch (MalformedURLException e) {
138
+
139
+ e.printStackTrace();
140
+
141
+ } catch (IOException e) {
142
+
143
+ e.printStackTrace();
144
+
145
+ }
146
+
147
+ }
148
+
149
+
150
+
151
+ public static void getNewAddress() {
152
+
153
+ try {
154
+
155
+ URL url = new URL(urlOperate + "/addresses");
156
+
157
+
158
+
159
+ HttpURLConnection conn = (HttpURLConnection)url.openConnection();
160
+
161
+ conn.setRequestMethod("POST");
162
+
163
+ conn.connect();
164
+
165
+
166
+
167
+ System.out.printf("Response: %d %s\n", conn.getResponseCode(), conn.getResponseMessage());
168
+
169
+ BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
170
+
171
+ String input;
172
+
173
+
174
+
175
+ while ((input = br.readLine()) != null) {
176
+
177
+ System.out.println(input);
178
+
179
+ }
180
+
181
+ br.close();
182
+
183
+ } catch (MalformedURLException e) {
184
+
185
+ e.printStackTrace();
186
+
187
+ } catch (IOException e) {
188
+
189
+ e.printStackTrace();
190
+
191
+ }
192
+
193
+ }
194
+
195
+
196
+
197
+ public static String getTxes(String txId) {
198
+
199
+ try {
200
+
17
- https://teratail.com/questions/134739
201
+ URL url = new URL(urlOperate + "/txes/" + txId);
202
+
203
+
204
+
205
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
206
+
207
+
208
+
209
+ System.out.printf("Response: %d %s\n", conn.getResponseCode(), conn.getResponseMessage());
210
+
211
+ BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
212
+
213
+ String input;
214
+
215
+
216
+
217
+ while ((input = br.readLine()) != null) {
218
+
219
+ System.out.println(input);
220
+
221
+ }
222
+
223
+
224
+
225
+ br.close();
226
+
227
+ return "";
228
+
229
+
230
+
231
+ } catch (MalformedURLException e) {
232
+
233
+ e.printStackTrace();
234
+
235
+ } catch (IOException e) {
236
+
237
+ e.printStackTrace();
238
+
239
+ }
240
+
241
+ return "";
242
+
243
+ }
244
+
245
+
246
+
247
+ public static String getAllTokensByAddress(String address) {
248
+
249
+ try {
250
+
251
+ URL url = new URL(urlOperate + "/addresses/" + address + "/tokens");
252
+
253
+
254
+
255
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
256
+
257
+
258
+
259
+ System.out.printf("Response: %d %s\n", conn.getResponseCode(), conn.getResponseMessage());
260
+
261
+ BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
262
+
263
+ String input;
264
+
265
+
266
+
267
+ while ((input = br.readLine()) != null) {
268
+
269
+ System.out.println(input);
270
+
271
+ }
272
+
273
+
274
+
275
+ br.close();
276
+
277
+ return "";
278
+
279
+
280
+
281
+ } catch (MalformedURLException e) {
282
+
283
+ e.printStackTrace();
284
+
285
+ } catch (IOException e) {
286
+
287
+ e.printStackTrace();
288
+
289
+ }
290
+
291
+ return "";
292
+
293
+ }
294
+
295
+ }
296
+
297
+ ```
18
298
 
19
299
 
20
300
 

2

コメント追加

2018/07/10 02:45

投稿

wangzj
wangzj

スコア53

test CHANGED
File without changes
test CHANGED
@@ -11,6 +11,10 @@
11
11
  HttpURLConnectionより、ブロックチェーンをアクセスして、Responseを返す
12
12
 
13
13
  ResponseはJsonデータなので、Jsonデータ処理(整形、Mapに転換など)が必要です。
14
+
15
+ 実際のコードなら、前の検証コードに関する質問をご参照すればと思います:
16
+
17
+ https://teratail.com/questions/134739
14
18
 
15
19
 
16
20
 

1

コメント追加

2018/07/10 02:26

投稿

wangzj
wangzj

スコア53

test CHANGED
File without changes
test CHANGED
@@ -33,3 +33,15 @@
33
33
  実現には可能でしょうか?
34
34
 
35
35
  ほかもっと簡単な方法があれば、ぜひご教授ください。
36
+
37
+
38
+
39
+ 開発環境:
40
+
41
+ windows
42
+
43
+ eclipse
44
+
45
+ サーバ環境:
46
+
47
+ ubuntu