質問編集履歴

1

追記

2018/02/09 05:19

投稿

JunZenpou
JunZenpou

スコア24

test CHANGED
File without changes
test CHANGED
@@ -8,118 +8,10 @@
8
8
 
9
9
 
10
10
 
11
- import android.content.Intent;
12
-
13
- import android.content.res.Configuration;
14
-
15
- import android.os.AsyncTask;
16
-
17
- import android.os.Bundle;
18
-
19
- import android.support.v4.widget.DrawerLayout;
20
-
21
- import android.support.v7.app.ActionBar;
22
-
23
- import android.support.v7.app.ActionBarDrawerToggle;
24
-
25
- import android.support.v7.app.AppCompatActivity;
26
-
27
- import android.view.MenuItem;
28
-
29
- import android.view.View;
30
-
31
- import android.widget.AdapterView;
32
-
33
- import android.widget.ArrayAdapter;
34
-
35
- import android.widget.EditText;
36
-
37
- import android.widget.ListView;
38
-
39
- import android.widget.TextView;
40
-
41
-
42
-
43
- import org.json.JSONException;
44
-
45
- import org.json.JSONObject;
46
-
47
-
48
-
49
- import java.io.BufferedReader;
50
-
51
- import java.io.IOException;
52
-
53
- import java.io.InputStreamReader;
54
-
55
- import java.io.OutputStream;
56
-
57
- import java.io.PrintStream;
58
-
59
- import java.math.BigInteger;
60
-
61
- import java.net.HttpURLConnection;
62
-
63
- import java.net.MalformedURLException;
64
-
65
- import java.net.URL;
66
-
67
- import java.security.MessageDigest;
68
-
69
-
70
-
71
11
  public class LoginActivity extends AppCompatActivity implements View.OnClickListener, ListView.OnItemClickListener {
72
12
 
73
13
 
74
14
 
75
- // ナビゲーションドロワーのトグル
76
-
77
- private ActionBarDrawerToggle mDrawerToggle;
78
-
79
-
80
-
81
- // listviewで表示するもの
82
-
83
- static String[] menuItems = { "ホーム", "ログイン" };
84
-
85
- static ArrayAdapter<String> adapter;
86
-
87
-
88
-
89
-
90
-
91
- // DrawerのListView
92
-
93
- private ListView drawerListView;
94
-
95
-
96
-
97
- private DrawerLayout drawerLayout;
98
-
99
-
100
-
101
- // editText
102
-
103
- private EditText loginText;
104
-
105
- private EditText passwdText;
106
-
107
-
108
-
109
- // textView
110
-
111
- private TextView connect_TextView;
112
-
113
- private TextView login_TextView;
114
-
115
- private TextView passwd_TextView;
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
15
  @Override
124
16
 
125
17
  protected void onCreate(Bundle savedInstanceState) {
@@ -430,12 +322,6 @@
430
322
 
431
323
  if (error != "error"){
432
324
 
433
- System.out.println("--------------------------------------------------error--------------------------------------------------------");
434
-
435
- System.out.println(error);
436
-
437
- System.out.println("--------------------------------------------------error--------------------------------------------------------");
438
-
439
325
  // 画面遷移
440
326
 
441
327
  Intent home = new Intent(this, HomeActivity.class);
@@ -485,3 +371,225 @@
485
371
 
486
372
 
487
373
  ```
374
+
375
+
376
+
377
+
378
+
379
+ ``` AsyckTask
380
+
381
+ // 非同期処理で通信処理を行うクラス
382
+
383
+ public class Request extends AsyncTask<String, Void, JSONObject> {
384
+
385
+ private TextView _connect_TextView;
386
+
387
+ private TextView _login_TextView;
388
+
389
+ private TextView _passwd_TextView;
390
+
391
+ private String _error;
392
+
393
+
394
+
395
+
396
+
397
+ // コンストラクタ
398
+
399
+ public Request(TextView mconnect_TextView, TextView mlogijn_TextView, TextView mpasswd_TextView, String merror){
400
+
401
+ _connect_TextView = mconnect_TextView;
402
+
403
+ _login_TextView = mlogijn_TextView;
404
+
405
+ _passwd_TextView = mpasswd_TextView;
406
+
407
+ _error = merror;
408
+
409
+ }
410
+
411
+
412
+
413
+ @Override
414
+
415
+ protected JSONObject doInBackground(String... params) {
416
+
417
+
418
+
419
+ // サーバーに送るjson
420
+
421
+ final String json = "{" +
422
+
423
+ "\"hash\":" + "\"" + params[0] + "\"," +
424
+
425
+ "\"user\":" + "{" + "\"loginid\":" + "\"" + params[1] + "\"," +
426
+
427
+ "\"passwd\":" + "\"" + params[2] + "\"" + "}" +
428
+
429
+ "}";
430
+
431
+
432
+
433
+ String result = null;
434
+
435
+
436
+
437
+ try{
438
+
439
+ String buffer = "";
440
+
441
+ // url
442
+
443
+ HttpURLConnection con = null;
444
+
445
+ URL url = new URL("url");
446
+
447
+ con = (HttpURLConnection) url.openConnection();
448
+
449
+
450
+
451
+ // POST送信
452
+
453
+ con.setRequestMethod("POST");
454
+
455
+
456
+
457
+ // リダイレクトボディの出力を行う
458
+
459
+ con.setDoOutput(true);
460
+
461
+
462
+
463
+ // リダイレクトの許可
464
+
465
+ con.setInstanceFollowRedirects(false);
466
+
467
+
468
+
469
+ // ヘッダーの設定
470
+
471
+ con.setRequestProperty("Content-Type", "application/json; charset=utf-8");
472
+
473
+
474
+
475
+ // 出力ストリーム
476
+
477
+ OutputStream os = con.getOutputStream();
478
+
479
+ PrintStream ps = new PrintStream(os);
480
+
481
+
482
+
483
+ ps.print(json);
484
+
485
+ ps.close();
486
+
487
+
488
+
489
+ BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
490
+
491
+ buffer = reader.readLine();
492
+
493
+ JSONObject jsonObject= new JSONObject(buffer);
494
+
495
+
496
+
497
+ return new JSONObject(buffer);
498
+
499
+
500
+
501
+ } catch (MalformedURLException e) {
502
+
503
+ e.printStackTrace();
504
+
505
+ } catch (IOException e) {
506
+
507
+ e.printStackTrace();
508
+
509
+ } catch (JSONException e) {
510
+
511
+ e.printStackTrace();
512
+
513
+ }
514
+
515
+
516
+
517
+ return null;
518
+
519
+ }
520
+
521
+
522
+
523
+
524
+
525
+ public void onPostExecute(JSONObject result) {
526
+
527
+ try {
528
+
529
+ if(result.getString("error") != null){
530
+
531
+ _connect_TextView.setText("IDかパスワードが間違っています。");
532
+
533
+ _error = "error";
534
+
535
+ }
536
+
537
+ } catch (JSONException e) {
538
+
539
+ e.printStackTrace();
540
+
541
+ }
542
+
543
+ }
544
+
545
+ }
546
+
547
+ }
548
+
549
+ ```
550
+
551
+
552
+
553
+ 上記のLoginActivityの
554
+
555
+ ```
556
+
557
+ // 通信処理
558
+
559
+ http_request(hash, loginid, passwd, error);
560
+
561
+
562
+
563
+
564
+
565
+ if (error != "error"){
566
+
567
+ // 画面遷移
568
+
569
+ Intent home = new Intent(this, HomeActivity.class);
570
+
571
+ startActivity(home);
572
+
573
+ }
574
+
575
+ ```
576
+
577
+ で、responseでエラーが帰ってこなかったら、画面遷移させて、エラーが帰ってきたら、画面遷移させない処理をしたいです。ここで、error変数はデータ通信のresponseをasyncTaskで代入しています。
578
+
579
+
580
+
581
+ asynkTaskが非同期なので、上記のhttp_request関数(asynkTaskクラスを含む)処理が終わる前に、次の処理にいってしまう?ため、error関数がresponseとして帰ってくる前に、画面遷移の処理が走ってしまいます。
582
+
583
+
584
+
585
+
586
+
587
+
588
+
589
+ なので、上記のhttp_request関数(asynkTaskクラスを含む)処理を非同期ではなく、同期処理をしたいのですが、どのようにコード作成をすれば良いでしょいか?
590
+
591
+
592
+
593
+
594
+
595
+ ご教授お願いします。