teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

コードの追加

2021/04/07 11:13

投稿

ToaZI
ToaZI

スコア39

title CHANGED
File without changes
body CHANGED
@@ -43,8 +43,59 @@
43
43
  ```
44
44
 
45
45
  ```AsyncTask
46
- public AsyncHttpRequest(Activity activity) {mActivity = activity;}
46
+ public AsyncHttpRequest(Activity activity) {
47
+ mActivity = activity;
48
+ }
49
+
50
+ @Override
51
+ protected void onPreExecute() {
52
+ super.onPreExecute();
53
+ }
54
+
55
+ @Override
56
+ protected void onPreExecute() {
57
+ super.onPreExecute();
58
+ }
59
+
60
+ @Override
61
+ protected String doInBackground(String... params) {
62
+
63
+ HttpURLConnection connection = null;
64
+ StringBuilder sb = new StringBuilder();
65
+ try {
66
+ URL url = new URL(params[0]);
67
+ connection = (HttpURLConnection) url.openConnection();
68
+ InputStream is = connection.getInputStream();
69
+ BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
70
+ String line = "";
71
+ while ((line = reader.readLine()) != null)
72
+ sb.append(line);
73
+ is.close();
74
+ } catch (IOException e) {
75
+ e.printStackTrace();
76
+ } finally {
77
+ connection.disconnect();
78
+ }
79
+ if (sb.toString() == "NO") {
80
+ return null;
81
+ } else {
82
+ return (sb.toString());
83
+ }
84
+ }
85
+
86
+ public void onPostExecute(String string) {
87
+ if (string == null) {
88
+ ((TextView)mActivity.findViewById(R.id.textView9)).setText("通信に失敗しました。再度接続をお試しください。");
89
+ } else {
90
+ try {
91
+ int i = Integer.parseInt(string);
47
- ((TextView) mActivity.findViewById(R.id.textView5)).setText(String.valueOf(i));
92
+ ((TextView)mActivity.findViewById(R.id.textView5)).setText(String.valueOf(i));
93
+ } catch (NumberFormatException e) {
94
+ }
95
+ }
96
+
97
+
98
+ }
48
99
  ```
49
100
 
50
101
  原因はおそらくmActivityの部分だと思うのですが、フラグメントに対して

1

コードの追加

2021/04/07 11:13

投稿

ToaZI
ToaZI

スコア39

title CHANGED
File without changes
body CHANGED
@@ -7,7 +7,39 @@
7
7
  at com.example.myapplication.ui.home.AsyncHttpRequest.onPostExecute(AsyncHttpRequest.java:62)
8
8
  ```
9
9
  ```DiarogFragment
10
+ @Override
11
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
12
+ AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
13
+ LayoutInflater inflater = requireActivity().getLayoutInflater();
14
+ View view = inflater.inflate (R.layout.custom_layout, null);
15
+ textView = (TextView)view.findViewById(R.id.textView5);
16
+ textView2 = (TextView)view.findViewById(R.id.textView6);
17
+ button = (Button)view.findViewById(R.id.button2);
18
+ button.setOnClickListener(new View.OnClickListener() {
19
+ @Override
20
+ public void onClick(View view) {
21
+ PW = rand.nextInt(100000);
22
+ if (new AsyncHttpRequest(getActivity())
10
- new AsyncHttpRequest(getActivity()).execute("https://xxxxxxxxxxxxxx.jp/xxxxxx)
23
+ .execute("https://xxxxxxxxxxxxxx.jp" + PW) != null) {
24
+ textView2.setText(String.valueOf(PW));
25
+ }
26
+ }
27
+ });
28
+ builder.setView(view)
29
+ .setTitle("Pairring")
30
+ .setPositiveButton("OK", new DialogInterface.OnClickListener() {
31
+ public void onClick(DialogInterface dialog, int id) {
32
+
33
+ }
34
+ })
35
+ .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
36
+ public void onClick(DialogInterface dialog, int id) {
37
+ // User cancelled the dialog
38
+ }
39
+ });
40
+
41
+ return builder.create();
42
+ }
11
43
  ```
12
44
 
13
45
  ```AsyncTask