質問編集履歴

9

修正

2019/10/14 04:23

投稿

KU_Slab19
KU_Slab19

スコア5

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,43 @@
1
+ アプリにある文字をサーバに上げたい。
2
+
3
+
4
+
5
+ 今、androidアプリを作っています。
6
+
7
+
8
+
9
+ 文字の読込はできていると思いますが、
10
+
11
+ 通信のやりかた、コードの書き方が合っているのか、わからない状況です。
12
+
13
+
14
+
15
+ アプリ開発超初心者なので、大変恐縮ですが
16
+
17
+ できるだけわかりやすいものをよろしくお願いいたします。
18
+
19
+
20
+
21
+ http://(adress) のwebサーバに上げていきたいです。
22
+
23
+ 先生が持っているサーバをお借りしたという形です。
24
+
25
+ ファイルにしなくても書き込みでもいいので、どんどん情報が追加されるような
26
+
27
+ ものにしたいです。
28
+
29
+
30
+
31
+ コードが合っているのであれば、サーバ自体に自分が上手くアクセスできていない可能性があるかなと思うので、アクセス権限などを確認します。
32
+
33
+
34
+
35
+ 調べてみるとPOST通信とかあるらしく、ネットからの情報で書いてみていますが、理解に苦しみました。
36
+
37
+ ご教授よろしくお願いします
38
+
39
+
40
+
1
41
  ```java
2
42
 
3
43
  package com.example.post_1009;
@@ -544,44 +584,6 @@
544
584
 
545
585
  ```
546
586
 
547
- ```アプリにある文字をサーバに上げたい。
587
+
548
-
549
-
550
-
551
- 今、androidアプリを作っています。
588
+
552
-
553
-
554
-
555
- 文字の読込できていると思いすが、
589
+ これで、エラーていません。
556
-
557
- 通信のやりかた、コードの書き方がわからない状況です。
558
-
559
-
560
-
561
- アプリ開発超初心者なので、大変恐縮ですが
562
-
563
- できるだけわかりやすいものをよろしくお願いいたします。
564
-
565
-
566
-
567
- (あまりに知識がなく、丸投げになってしまい申しわけありません。)
568
-
569
- 追加します。
570
-
571
-
572
-
573
- http://(adress) のwebサーバに上げていきたいです。
574
-
575
- 先生が持っているサーバをお借りしたという形です。
576
-
577
- このサーバにデータを追加するには、承認みたいなものがいるのか、
578
-
579
- ファイルにしなくても書き込みでもいいので、どんどん情報が追加されるような
580
-
581
- ものにしたいです。
582
-
583
-
584
-
585
- 調べてみるとPOST通信とかあるみたいですが、理解に苦しみました。
586
-
587
- ご教授よろしくお願いします

8

コード修正

2019/10/14 04:23

投稿

KU_Slab19
KU_Slab19

スコア5

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- ```MainActivity.java
1
+ ```java
2
2
 
3
3
  package com.example.post_1009;
4
4
 
@@ -174,7 +174,7 @@
174
174
 
175
175
  ```
176
176
 
177
- ```UploadTak.java
177
+ ```java
178
178
 
179
179
  package com.example.post_1009;
180
180
 
@@ -372,7 +372,7 @@
372
372
 
373
373
  ```
374
374
 
375
- ```activity_main.xml
375
+ ```xml
376
376
 
377
377
  <?xml version="1.0" encoding="utf-8"?>
378
378
 

7

コード追加

2019/10/14 03:46

投稿

KU_Slab19
KU_Slab19

スコア5

test CHANGED
File without changes
test CHANGED
@@ -52,7 +52,7 @@
52
52
 
53
53
  // phpがPOSTで受け取ったwordを入れて作成するHTMLページ(適宜合わせてください)
54
54
 
55
- String url = "http://192.168.53.4/";
55
+ String url = "http://address/";
56
56
 
57
57
 
58
58
 
@@ -214,7 +214,7 @@
214
214
 
215
215
  // 使用するサーバーのURLに合わせる
216
216
 
217
- String urlSt = "http://192.168.53.4/";
217
+ String urlSt = "http://address/";
218
218
 
219
219
 
220
220
 
@@ -550,15 +550,9 @@
550
550
 
551
551
  今、androidアプリを作っています。
552
552
 
553
- Spinnerから選択したものの文字を読み込んで、
553
+
554
-
555
- ボタン押下時に、選んだものと時刻をwebサーバに上げるというような
554
+
556
-
557
- をしたのですが、方法がわかりません。
555
+ 文字の読込はできているすが、
558
-
559
-
560
-
561
- 今のところ、Spinnerの選択したもの、時刻を読み込むことはできているので
562
556
 
563
557
  通信のやりかた、コードの書き方がわからない状況です。
564
558
 
@@ -576,12 +570,6 @@
576
570
 
577
571
 
578
572
 
579
-
580
-
581
- この決定ボタンを押したときに、Spinnerで選択した項目と、押した時間を
582
-
583
- テキストファイルで
584
-
585
573
  http://(adress) のwebサーバに上げていきたいです。
586
574
 
587
575
  先生が持っているサーバをお借りしたという形です。

6

コード追加

2019/10/14 03:28

投稿

KU_Slab19
KU_Slab19

スコア5

test CHANGED
File without changes
test CHANGED
@@ -172,9 +172,9 @@
172
172
 
173
173
  }
174
174
 
175
-
175
+ ```
176
-
176
+
177
- ```UploadTask.java
177
+ ```UploadTak.java
178
178
 
179
179
  package com.example.post_1009;
180
180
 
@@ -368,6 +368,8 @@
368
368
 
369
369
  }
370
370
 
371
+
372
+
371
373
  ```
372
374
 
373
375
  ```activity_main.xml

5

コード追加

2019/10/14 03:25

投稿

KU_Slab19
KU_Slab19

スコア5

test CHANGED
File without changes
test CHANGED
@@ -368,8 +368,6 @@
368
368
 
369
369
  }
370
370
 
371
-
372
-
373
371
  ```
374
372
 
375
373
  ```activity_main.xml

4

コード追加

2019/10/14 03:23

投稿

KU_Slab19
KU_Slab19

スコア5

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,550 @@
1
+ ```MainActivity.java
2
+
3
+ package com.example.post_1009;
4
+
5
+
6
+
7
+ import androidx.appcompat.app.AppCompatActivity;
8
+
9
+
10
+
11
+ import android.os.Bundle;
12
+
13
+ import androidx.appcompat.app.AppCompatActivity;
14
+
15
+ //import android.support.v7.app.AppCompatActivity;
16
+
17
+
18
+
19
+ import android.content.Intent;
20
+
21
+ import android.net.Uri;
22
+
23
+ import android.os.Bundle;
24
+
25
+ import android.view.View;
26
+
27
+ import android.widget.Button;
28
+
29
+ import android.widget.EditText;
30
+
31
+ import android.widget.TextView;
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+ public class MainActivity extends AppCompatActivity {
40
+
41
+
42
+
43
+ private UploadTask task;
44
+
45
+ private TextView textView;
46
+
47
+ // wordを入れる
48
+
49
+ private EditText editText;
50
+
51
+
52
+
53
+ // phpがPOSTで受け取ったwordを入れて作成するHTMLページ(適宜合わせてください)
54
+
55
+ String url = "http://192.168.53.4/";
56
+
57
+
58
+
59
+ @Override
60
+
61
+ protected void onCreate(Bundle savedInstanceState) {
62
+
63
+ super.onCreate(savedInstanceState);
64
+
65
+ setContentView(R.layout.activity_main);
66
+
67
+
68
+
69
+ editText = findViewById(R.id.uriname);
70
+
71
+
72
+
73
+ Button post = findViewById(R.id.post);
74
+
75
+
76
+
77
+ // ボタンをタップして非同期処理を開始
78
+
79
+ post.setOnClickListener(new View.OnClickListener() {
80
+
81
+ @Override
82
+
83
+ public void onClick(View v) {
84
+
85
+ String param0 = editText.getText().toString();
86
+
87
+
88
+
89
+ if(param0.length() != 0){
90
+
91
+ task = new UploadTask();
92
+
93
+ task.setListener(createListener());
94
+
95
+ task.execute(param0);
96
+
97
+ }
98
+
99
+
100
+
101
+ }
102
+
103
+ });
104
+
105
+
106
+
107
+ // ブラウザを起動する
108
+
109
+ Button browser = findViewById(R.id.browser);
110
+
111
+ browser.setOnClickListener(new View.OnClickListener() {
112
+
113
+ @Override
114
+
115
+ public void onClick(View v) {
116
+
117
+ // phpで作成されたhtmlファイルへアクセス
118
+
119
+ Uri uri = Uri.parse(url);
120
+
121
+ Intent intent = new Intent(Intent.ACTION_VIEW,uri);
122
+
123
+ startActivity(intent);
124
+
125
+
126
+
127
+ // text clear
128
+
129
+ textView.setText("");
130
+
131
+ }
132
+
133
+ });
134
+
135
+
136
+
137
+ textView = findViewById(R.id.text_view);
138
+
139
+ }
140
+
141
+
142
+
143
+
144
+
145
+ @Override
146
+
147
+ protected void onDestroy() {
148
+
149
+ task.setListener(null);
150
+
151
+ super.onDestroy();
152
+
153
+ }
154
+
155
+
156
+
157
+ private UploadTask.Listener createListener() {
158
+
159
+ return new UploadTask.Listener() {
160
+
161
+ @Override
162
+
163
+ public void onSuccess(String result) {
164
+
165
+ textView.setText(result);
166
+
167
+ }
168
+
169
+ };
170
+
171
+ }
172
+
173
+ }
174
+
175
+
176
+
177
+ ```UploadTask.java
178
+
179
+ package com.example.post_1009;
180
+
181
+ import android.os.AsyncTask;
182
+
183
+ import android.util.Log;
184
+
185
+ import java.io.IOException;
186
+
187
+ import java.io.OutputStream;
188
+
189
+ import java.net.HttpURLConnection;
190
+
191
+ import java.net.URL;
192
+
193
+ import java.nio.charset.StandardCharsets;
194
+
195
+
196
+
197
+
198
+
199
+ public class UploadTask extends AsyncTask<String, Void, String> {
200
+
201
+
202
+
203
+ private Listener listener;
204
+
205
+
206
+
207
+ // 非同期処理
208
+
209
+ @Override
210
+
211
+ protected String doInBackground(String... params) {
212
+
213
+
214
+
215
+ // 使用するサーバーのURLに合わせる
216
+
217
+ String urlSt = "http://192.168.53.4/";
218
+
219
+
220
+
221
+ HttpURLConnection httpConn = null;
222
+
223
+ String result = null;
224
+
225
+ String word = "word="+params[0];
226
+
227
+
228
+
229
+ try{
230
+
231
+ // URL設定
232
+
233
+ URL url = new URL(urlSt);
234
+
235
+
236
+
237
+ // HttpURLConnection
238
+
239
+ httpConn = (HttpURLConnection) url.openConnection();
240
+
241
+
242
+
243
+ // request POST
244
+
245
+ httpConn.setRequestMethod("POST");
246
+
247
+
248
+
249
+ // no Redirects
250
+
251
+ httpConn.setInstanceFollowRedirects(false);
252
+
253
+
254
+
255
+ // データを書き込む
256
+
257
+ httpConn.setDoOutput(true);
258
+
259
+
260
+
261
+ // 時間制限
262
+
263
+ httpConn.setReadTimeout(10000);
264
+
265
+ httpConn.setConnectTimeout(20000);
266
+
267
+
268
+
269
+ // 接続
270
+
271
+ httpConn.connect();
272
+
273
+
274
+
275
+ try(// POSTデータ送信処理
276
+
277
+ OutputStream outStream = httpConn.getOutputStream()) {
278
+
279
+ outStream.write( word.getBytes(StandardCharsets.UTF_8));
280
+
281
+ outStream.flush();
282
+
283
+ Log.d("debug","flush");
284
+
285
+ } catch (IOException e) {
286
+
287
+ // POST送信エラー
288
+
289
+ e.printStackTrace();
290
+
291
+ result = "POST送信エラー";
292
+
293
+ }
294
+
295
+
296
+
297
+ final int status = httpConn.getResponseCode();
298
+
299
+ if (status == HttpURLConnection.HTTP_OK) {
300
+
301
+ // レスポンスを受け取る処理等
302
+
303
+ result="HTTP_OK";
304
+
305
+ }
306
+
307
+ else{
308
+
309
+ result="status="+String.valueOf(status);
310
+
311
+ }
312
+
313
+
314
+
315
+ } catch (IOException e) {
316
+
317
+ e.printStackTrace();
318
+
319
+ } finally {
320
+
321
+ if (httpConn != null) {
322
+
323
+ httpConn.disconnect();
324
+
325
+ }
326
+
327
+ }
328
+
329
+ return result;
330
+
331
+ }
332
+
333
+
334
+
335
+ // 非同期処理が終了後、結果をメインスレッドに返す
336
+
337
+ @Override
338
+
339
+ protected void onPostExecute(String result) {
340
+
341
+ super.onPostExecute(result);
342
+
343
+
344
+
345
+ if (listener != null) {
346
+
347
+ listener.onSuccess(result);
348
+
349
+ }
350
+
351
+ }
352
+
353
+
354
+
355
+ void setListener(Listener listener) {
356
+
357
+ this.listener = listener;
358
+
359
+ }
360
+
361
+
362
+
363
+ interface Listener {
364
+
365
+ void onSuccess(String result);
366
+
367
+ }
368
+
369
+ }
370
+
371
+
372
+
373
+ ```
374
+
375
+ ```activity_main.xml
376
+
377
+ <?xml version="1.0" encoding="utf-8"?>
378
+
379
+ <!--AndroidX-->
380
+
381
+ <androidx.constraintlayout.widget.ConstraintLayout
382
+
383
+ xmlns:android="http://schemas.android.com/apk/res/android"
384
+
385
+ xmlns:app="http://schemas.android.com/apk/res-auto"
386
+
387
+ xmlns:tools="http://schemas.android.com/tools"
388
+
389
+ android:layout_width="match_parent"
390
+
391
+ android:layout_height="match_parent"
392
+
393
+ android:background="#fdc"
394
+
395
+ tools:context=".MainActivity">
396
+
397
+
398
+
399
+ <TextView
400
+
401
+ android:id="@+id/word"
402
+
403
+ android:layout_width="0dp"
404
+
405
+ android:layout_height="wrap_content"
406
+
407
+ android:layout_marginEnd="10dp"
408
+
409
+ android:text="@string/word"
410
+
411
+ android:textSize="24sp"
412
+
413
+ app:layout_constraintBottom_toBottomOf="parent"
414
+
415
+ app:layout_constraintEnd_toEndOf="@+id/guideline"
416
+
417
+ app:layout_constraintLeft_toLeftOf="parent"
418
+
419
+ app:layout_constraintTop_toTopOf="parent"
420
+
421
+ app:layout_constraintVertical_bias="0.15" />
422
+
423
+
424
+
425
+ <EditText
426
+
427
+ android:id="@+id/uriname"
428
+
429
+ android:layout_width="0dp"
430
+
431
+ android:layout_height="wrap_content"
432
+
433
+ android:autofillHints="@string/hint"
434
+
435
+ android:background="#fff"
436
+
437
+ android:hint="@string/hint"
438
+
439
+ android:inputType="textUri"
440
+
441
+ android:textSize="24sp"
442
+
443
+ app:layout_constraintStart_toStartOf="@+id/guideline"
444
+
445
+ app:layout_constraintBottom_toBottomOf="parent"
446
+
447
+ app:layout_constraintRight_toRightOf="parent"
448
+
449
+ app:layout_constraintTop_toTopOf="parent"
450
+
451
+ app:layout_constraintVertical_bias="0.15" />
452
+
453
+
454
+
455
+ <Button
456
+
457
+ android:id="@+id/post"
458
+
459
+ android:layout_width="0dp"
460
+
461
+ android:layout_height="wrap_content"
462
+
463
+ android:layout_marginStart="30dp"
464
+
465
+ android:layout_marginEnd="10dp"
466
+
467
+ android:text="@string/post"
468
+
469
+ app:layout_constraintTop_toTopOf="parent"
470
+
471
+ app:layout_constraintRight_toLeftOf="@+id/browser"
472
+
473
+ app:layout_constraintLeft_toLeftOf="parent"
474
+
475
+ app:layout_constraintBottom_toBottomOf="parent"
476
+
477
+ app:layout_constraintVertical_bias="0.3" />
478
+
479
+
480
+
481
+ <Button
482
+
483
+ android:id="@+id/browser"
484
+
485
+ android:layout_width="0dp"
486
+
487
+ android:layout_height="wrap_content"
488
+
489
+ android:layout_marginEnd="30dp"
490
+
491
+ android:layout_marginStart="10dp"
492
+
493
+ android:text="@string/browser"
494
+
495
+ app:layout_constraintLeft_toRightOf="@+id/post"
496
+
497
+ app:layout_constraintTop_toTopOf="parent"
498
+
499
+ app:layout_constraintBottom_toBottomOf="parent"
500
+
501
+ app:layout_constraintRight_toRightOf="parent"
502
+
503
+ app:layout_constraintVertical_bias="0.3" />
504
+
505
+
506
+
507
+ <TextView
508
+
509
+ android:id="@+id/text_view"
510
+
511
+ android:layout_width="wrap_content"
512
+
513
+ android:layout_height="wrap_content"
514
+
515
+ android:textSize="24sp"
516
+
517
+ app:layout_constraintTop_toTopOf="parent"
518
+
519
+ app:layout_constraintRight_toRightOf="parent"
520
+
521
+ app:layout_constraintLeft_toLeftOf="parent"
522
+
523
+ app:layout_constraintBottom_toBottomOf="parent"
524
+
525
+ app:layout_constraintVertical_bias="0.4" />
526
+
527
+ <!--AndroidX-->
528
+
529
+ <androidx.constraintlayout.widget.Guideline
530
+
531
+ android:id="@+id/guideline"
532
+
533
+ android:layout_width="wrap_content"
534
+
535
+ android:layout_height="wrap_content"
536
+
537
+ android:orientation="vertical"
538
+
539
+ app:layout_constraintGuide_begin="100dp" />
540
+
541
+
542
+
543
+ </androidx.constraintlayout.widget.ConstraintLayout>
544
+
545
+ ```
546
+
1
- アプリにある文字をサーバに上げたい。
547
+ ```アプリにある文字をサーバに上げたい。
2
548
 
3
549
 
4
550
 
@@ -30,58 +576,6 @@
30
576
 
31
577
 
32
578
 
33
- 現状
34
-
35
- <?xml version="1.0" encoding="utf-8"?>
36
-
37
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
38
-
39
- android:orientation="vertical"
40
-
41
- android:layout_width="match_parent"
42
-
43
- android:layout_height="match_parent">
44
-
45
-
46
-
47
- <Spinner
48
-
49
- android:id="@+id/Spinner_shusseki"
50
-
51
- android:layout_width="200dp"
52
-
53
- android:layout_height="80dp"
54
-
55
- android:layout_gravity="center"
56
-
57
- android:entries="@array/shusseki"
58
-
59
- android:layout_marginTop="100dp"
60
-
61
- android:background="@drawable/waku_spinner"
62
-
63
- android:spinnerMode="dialog">
64
-
65
- </Spinner>
66
-
67
-
68
-
69
- <Button
70
-
71
- android:id="@+id/kettei"
72
-
73
- android:layout_width="200dp"
74
-
75
- android:layout_height="80dp"
76
-
77
- android:layout_gravity="center"
78
-
79
- android:text="決定"
80
-
81
- android:layout_marginTop="100dp"
82
-
83
- android:background="@drawable/style_circle" />
84
-
85
579
 
86
580
 
87
581
  この決定ボタンを押したときに、Spinnerで選択した項目と、押した時間を

3

質問追加

2019/10/14 03:20

投稿

KU_Slab19
KU_Slab19

スコア5

test CHANGED
File without changes
test CHANGED
@@ -98,6 +98,8 @@
98
98
 
99
99
  ものにしたいです。
100
100
 
101
+
102
+
101
103
  調べてみるとPOST通信とかあるみたいですが、理解に苦しみました。
102
104
 
103
105
  ご教授よろしくお願いします

2

情報追加

2019/10/14 02:57

投稿

KU_Slab19
KU_Slab19

スコア5

test CHANGED
File without changes
test CHANGED
@@ -98,4 +98,6 @@
98
98
 
99
99
  ものにしたいです。
100
100
 
101
+ 調べてみるとPOST通信とかあるみたいですが、理解に苦しみました。
102
+
101
103
  ご教授よろしくお願いします

1

情報追加

2019/10/14 02:51

投稿

KU_Slab19
KU_Slab19

スコア5

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  Spinnerから選択したものの文字を読み込んで、
8
8
 
9
- ボタン押下時に、選んだものと時刻をサーバに上げるというような
9
+ ボタン押下時に、選んだものと時刻をwebサーバに上げるというような
10
10
 
11
11
  ことをしたいのですが、方法がわかりません。
12
12
 
@@ -21,3 +21,81 @@
21
21
  アプリ開発超初心者なので、大変恐縮ですが
22
22
 
23
23
  できるだけわかりやすいものをよろしくお願いいたします。
24
+
25
+
26
+
27
+ (あまりに知識がなく、丸投げになってしまい申しわけありません。)
28
+
29
+ 追加します。
30
+
31
+
32
+
33
+ 現状
34
+
35
+ <?xml version="1.0" encoding="utf-8"?>
36
+
37
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
38
+
39
+ android:orientation="vertical"
40
+
41
+ android:layout_width="match_parent"
42
+
43
+ android:layout_height="match_parent">
44
+
45
+
46
+
47
+ <Spinner
48
+
49
+ android:id="@+id/Spinner_shusseki"
50
+
51
+ android:layout_width="200dp"
52
+
53
+ android:layout_height="80dp"
54
+
55
+ android:layout_gravity="center"
56
+
57
+ android:entries="@array/shusseki"
58
+
59
+ android:layout_marginTop="100dp"
60
+
61
+ android:background="@drawable/waku_spinner"
62
+
63
+ android:spinnerMode="dialog">
64
+
65
+ </Spinner>
66
+
67
+
68
+
69
+ <Button
70
+
71
+ android:id="@+id/kettei"
72
+
73
+ android:layout_width="200dp"
74
+
75
+ android:layout_height="80dp"
76
+
77
+ android:layout_gravity="center"
78
+
79
+ android:text="決定"
80
+
81
+ android:layout_marginTop="100dp"
82
+
83
+ android:background="@drawable/style_circle" />
84
+
85
+
86
+
87
+ この決定ボタンを押したときに、Spinnerで選択した項目と、押した時間を
88
+
89
+ テキストファイルで
90
+
91
+ http://(adress) のwebサーバに上げていきたいです。
92
+
93
+ 先生が持っているサーバをお借りしたという形です。
94
+
95
+ このサーバにデータを追加するには、承認みたいなものがいるのか、
96
+
97
+ ファイルにしなくても書き込みでもいいので、どんどん情報が追加されるような
98
+
99
+ ものにしたいです。
100
+
101
+ ご教授よろしくお願いします