質問編集履歴

5

修正

2018/02/26 14:40

投稿

eisaku123
eisaku123

スコア74

test CHANGED
File without changes
test CHANGED
@@ -156,7 +156,7 @@
156
156
 
157
157
  ) throws ServletException, IOException {
158
158
 
159
- response.setContentType("text/html ;charset=UTF-8");
159
+ response.setContentType("image/jpeg ;charset=UTF-8");
160
160
 
161
161
  response.setHeader("Content-Disposition" ,"attachment");
162
162
 

4

再編集

2018/02/26 14:40

投稿

eisaku123
eisaku123

スコア74

test CHANGED
File without changes
test CHANGED
@@ -26,13 +26,11 @@
26
26
 
27
27
 
28
28
 
29
- //クライアント側 追加
29
+ //クライアント側 再編集 
30
-
30
+
31
- public class AsyncNetworkTask extends AsyncTask<String, Integer, String> {
31
+ public class AsyncNetworkTask extends AsyncTask<Void, Void, byte[]> {
32
-
33
- private TextView txtResult;
32
+
34
-
35
- private ProgressBar progress;
33
+
36
34
 
37
35
  private ImageView imageview;
38
36
 
@@ -44,11 +42,7 @@
44
42
 
45
43
  MainActivity activity = (MainActivity)context;
46
44
 
47
- txtResult = (TextView)activity.findViewById(R.id.txtResult);
48
-
49
- progress = (ProgressBar)activity.findViewById(R.id.progress);
50
-
51
- imageview = (ImageView)activity.findViewById(R.id.iv); //追加
45
+ imageview = (ImageView)activity.findViewById(R.id.iv);
52
46
 
53
47
  }
54
48
 
@@ -56,25 +50,23 @@
56
50
 
57
51
  @Override
58
52
 
59
- protected String doInBackground(String... params) {
53
+ protected byte[] doInBackground(Void... voids) {
60
-
61
- publishProgress(30);
54
+
62
-
55
+
56
+
63
- SystemClock.sleep(1000);
57
+ byte[] data;
64
-
65
- publishProgress(60);
58
+
59
+
66
60
 
67
61
  StringBuilder builder = new StringBuilder();
68
62
 
69
63
  try {
70
64
 
71
- URL url = new URL(params[0]);
65
+ URL url = new URL("http://192.168.3.5:8080/book777/chapter3");
72
66
 
73
67
  HttpURLConnection con = (HttpURLConnection)url.openConnection();
74
68
 
75
- con.setRequestMethod("GET");
69
+ con.setRequestMethod("GET");
76
-
77
-
78
70
 
79
71
  BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
80
72
 
@@ -86,20 +78,52 @@
86
78
 
87
79
  }
88
80
 
89
-
90
-
91
- } catch (IOException e) {
81
+ } catch (IOException e) {
92
82
 
93
83
  e.printStackTrace();
94
84
 
95
85
  }
96
86
 
97
- publishProgress(100);
87
+
98
-
88
+
99
- return builder.toString();
89
+ String i = builder.toString();
90
+
91
+ data=i.getBytes();
92
+
93
+ return data;
100
94
 
101
95
  }
102
96
 
97
+
98
+
99
+ @Override
100
+
101
+ protected void onPostExecute(byte[] result) {
102
+
103
+ Bitmap bmp2 = null;
104
+
105
+
106
+
107
+ if (result != null) { //byte配列をBitmapオブジェクトに変換する
108
+
109
+ bmp2 = BitmapFactory.decodeByteArray(result, 0, result.length);//nullになってしまう
110
+
111
+ }
112
+
113
+ if (bmp2 == null){ //bmp2がnullかどうか調べる
114
+
115
+ imageview.setImageResource(R.drawable.monster);//<----ここに入ってしまう!!!
116
+
117
+ }
118
+
119
+
120
+
121
+ imageview.setImageBitmap(bmp2);//nullになってしまうため画像が表示されない
122
+
123
+ }
124
+
125
+ }
126
+
103
127
  ```
104
128
 
105
129
 
@@ -108,118 +132,86 @@
108
132
 
109
133
 
110
134
 
135
+
136
+
137
+
138
+
111
139
  ```java
112
140
 
113
- //クライアント
114
-
115
- @Override
116
-
117
- protected void onPostExecute(String result) {
118
-
119
-
120
-
121
- Bitmap bmp2 = null;
122
-
123
-
124
-
125
- byte[] sbyte = result.getBytes(); //String-->byteに置き換える
126
-
127
-
128
-
129
- if (sbyte != null) {
130
-
131
- bmp2 = BitmapFactory.decodeByteArray(sbyte, 0, sbyte.length); //<===ここで戻り値、nullになってしまう!!!
132
-
133
- }
134
-
135
-
136
-
137
- imageview.setImageBitmap(bmp2);
138
-
139
- }
140
-
141
-
141
+ //サーバー
142
+
143
+ @WebServlet(urlPatterns={"/chapter3/*"})
144
+
145
+ public class Hello3 extends HttpServlet{
146
+
147
+
148
+
149
+ @Override
150
+
151
+ public void doGet(
152
+
153
+ HttpServletRequest request,
154
+
155
+ HttpServletResponse response
156
+
157
+ ) throws ServletException, IOException {
158
+
159
+ response.setContentType("text/html ;charset=UTF-8");
160
+
161
+ response.setHeader("Content-Disposition" ,"attachment");
162
+
163
+
164
+
165
+ OutputStream out = response.getOutputStream();//ここでストリームをつなげる
166
+
167
+
168
+
169
+ try {
170
+
171
+ FileInputStream input =new FileInputStream("/Applications/Eclipse_4.7.2.app/Contents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/book777/WEB-INF/classes/chapter3/1.jpg");
172
+
173
+
174
+
175
+
176
+
177
+ int i = input.read();
178
+
179
+ while(i !=-1) {
180
+
181
+ char c = (char)i;
182
+
183
+ out.write(c);//ここでクライアントにデータを流す
184
+
185
+ System.out.print(c)
186
+
187
+ i = input.read();
188
+
189
+ }
190
+
191
+ input.close();
192
+
193
+ out.close();
194
+
195
+
196
+
197
+ }catch(Exception e) {
198
+
199
+ System.out.println("意味不明のエラーです");
200
+
201
+ }catch (Throwable e) {
202
+
203
+ System.out.println("byte[] data = IOUtils.toByteArray(input);でエラーです");
204
+
205
+ }
206
+
207
+ }
208
+
209
+
210
+
211
+ }
142
212
 
143
213
  ```
144
214
 
145
215
 
146
216
 
147
- ```java
148
-
149
- //サーバー側
150
-
151
- @WebServlet(urlPatterns={"/chapter3/*"})
152
-
153
- public class Hello3 extends HttpServlet{
154
-
155
-
156
-
157
- @Override
158
-
159
- public void doGet(
160
-
161
- HttpServletRequest request,
162
-
163
- HttpServletResponse response
164
-
165
- ) throws ServletException, IOException {
166
-
167
- response.setContentType("text/html ;charset=UTF-8");
168
-
169
- response.setHeader("Content-Disposition" ,"attachment");
170
-
171
-
172
-
173
- OutputStream out = response.getOutputStream();//ここでストリームをつなげる
174
-
175
-
176
-
177
- try {
178
-
179
- FileInputStream input =new FileInputStream("/Applications/Eclipse_4.7.2.app/Contents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/book777/WEB-INF/classes/chapter3/1.jpg");
180
-
181
-
182
-
183
-
184
-
185
- int i = input.read();
186
-
187
- while(i !=-1) {
188
-
189
- char c = (char)i;
190
-
191
- out.write(c);//ここでクライアントにデータを流す
192
-
193
- System.out.print(c)
194
-
195
- i = input.read();
196
-
197
- }
198
-
199
- input.close();
200
-
201
- out.close();
202
-
203
-
204
-
205
- }catch(Exception e) {
206
-
207
- System.out.println("意味不明のエラーです");
208
-
209
- }catch (Throwable e) {
210
-
211
- System.out.println("byte[] data = IOUtils.toByteArray(input);でエラーです");
212
-
213
- }
214
-
215
- }
216
-
217
-
218
-
219
- }
220
-
221
- ```
222
-
223
-
224
-
225
217
  ������JFIF��,,��������C��  (1#%(:3=<9387@H\N@DWE78PmQW_bghg>Mqypdx\egc����C//cB8Bcccccccccccccccccccccccccccccccccccccccccccccccccc�������"�������������������������� �����������}��!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������������������������������������������������������������� ���������w��!1AQaq"2�B���� #3R�br�$4�

3

追加

2018/02/26 13:31

投稿

eisaku123
eisaku123

スコア74

test CHANGED
File without changes
test CHANGED
@@ -24,6 +24,92 @@
24
24
 
25
25
  ```java
26
26
 
27
+
28
+
29
+ //クライアント側 追加
30
+
31
+ public class AsyncNetworkTask extends AsyncTask<String, Integer, String> {
32
+
33
+ private TextView txtResult;
34
+
35
+ private ProgressBar progress;
36
+
37
+ private ImageView imageview;
38
+
39
+
40
+
41
+ public AsyncNetworkTask(Context context) {
42
+
43
+ super();
44
+
45
+ MainActivity activity = (MainActivity)context;
46
+
47
+ txtResult = (TextView)activity.findViewById(R.id.txtResult);
48
+
49
+ progress = (ProgressBar)activity.findViewById(R.id.progress);
50
+
51
+ imageview = (ImageView)activity.findViewById(R.id.iv); //追加
52
+
53
+ }
54
+
55
+
56
+
57
+ @Override
58
+
59
+ protected String doInBackground(String... params) {
60
+
61
+ publishProgress(30);
62
+
63
+ SystemClock.sleep(1000);
64
+
65
+ publishProgress(60);
66
+
67
+ StringBuilder builder = new StringBuilder();
68
+
69
+ try {
70
+
71
+ URL url = new URL(params[0]);
72
+
73
+ HttpURLConnection con = (HttpURLConnection)url.openConnection();
74
+
75
+ con.setRequestMethod("GET");
76
+
77
+
78
+
79
+ BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
80
+
81
+ String line;
82
+
83
+ while ((line = reader.readLine()) != null){
84
+
85
+ builder.append(line);
86
+
87
+ }
88
+
89
+
90
+
91
+ } catch (IOException e) {
92
+
93
+ e.printStackTrace();
94
+
95
+ }
96
+
97
+ publishProgress(100);
98
+
99
+ return builder.toString();
100
+
101
+ }
102
+
103
+ ```
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+ ```java
112
+
27
113
  //クライアント側
28
114
 
29
115
  @Override

2

サーバ側、追記

2018/02/26 09:50

投稿

eisaku123
eisaku123

スコア74

test CHANGED
File without changes
test CHANGED
@@ -12,11 +12,19 @@
12
12
 
13
13
  resultのデータはサーバからくるデータと考えおります。
14
14
 
15
+
16
+
17
+ サーバー側のソースは以下の以下の通り
18
+
19
+
20
+
15
- ちなみにresultからくるデータは以下の以下の通り。
21
+ ちなみにresultからくるデータは以下の以下の以下通り。
16
22
 
17
23
 
18
24
 
19
25
  ```java
26
+
27
+ //クライアント側
20
28
 
21
29
  @Override
22
30
 
@@ -50,4 +58,82 @@
50
58
 
51
59
 
52
60
 
61
+ ```java
62
+
63
+ //サーバー側
64
+
65
+ @WebServlet(urlPatterns={"/chapter3/*"})
66
+
67
+ public class Hello3 extends HttpServlet{
68
+
69
+
70
+
71
+ @Override
72
+
73
+ public void doGet(
74
+
75
+ HttpServletRequest request,
76
+
77
+ HttpServletResponse response
78
+
79
+ ) throws ServletException, IOException {
80
+
81
+ response.setContentType("text/html ;charset=UTF-8");
82
+
83
+ response.setHeader("Content-Disposition" ,"attachment");
84
+
85
+
86
+
87
+ OutputStream out = response.getOutputStream();//ここでストリームをつなげる
88
+
89
+
90
+
91
+ try {
92
+
93
+ FileInputStream input =new FileInputStream("/Applications/Eclipse_4.7.2.app/Contents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/book777/WEB-INF/classes/chapter3/1.jpg");
94
+
95
+
96
+
97
+
98
+
99
+ int i = input.read();
100
+
101
+ while(i !=-1) {
102
+
103
+ char c = (char)i;
104
+
105
+ out.write(c);//ここでクライアントにデータを流す
106
+
107
+ System.out.print(c)
108
+
109
+ i = input.read();
110
+
111
+ }
112
+
113
+ input.close();
114
+
115
+ out.close();
116
+
117
+
118
+
119
+ }catch(Exception e) {
120
+
121
+ System.out.println("意味不明のエラーです");
122
+
123
+ }catch (Throwable e) {
124
+
125
+ System.out.println("byte[] data = IOUtils.toByteArray(input);でエラーです");
126
+
127
+ }
128
+
129
+ }
130
+
131
+
132
+
133
+ }
134
+
135
+ ```
136
+
137
+
138
+
53
139
  ������JFIF��,,��������C��  (1#%(:3=<9387@H\N@DWE78PmQW_bghg>Mqypdx\egc����C//cB8Bcccccccccccccccccccccccccccccccccccccccccccccccccc�������"�������������������������� �����������}��!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������������������������������������������������������������� ���������w��!1AQaq"2�B���� #3R�br�$4�

1

@Override

2018/02/26 09:19

投稿

eisaku123
eisaku123

スコア74

test CHANGED
File without changes
test CHANGED
@@ -17,6 +17,8 @@
17
17
 
18
18
 
19
19
  ```java
20
+
21
+ @Override
20
22
 
21
23
  protected void onPostExecute(String result) {
22
24