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

質問編集履歴

5

修正

2018/02/26 14:40

投稿

eisaku123
eisaku123

スコア77

title CHANGED
File without changes
body CHANGED
@@ -77,7 +77,7 @@
77
77
  HttpServletRequest request,
78
78
  HttpServletResponse response
79
79
  ) throws ServletException, IOException {
80
- response.setContentType("text/html ;charset=UTF-8");
80
+ response.setContentType("image/jpeg ;charset=UTF-8");
81
81
  response.setHeader("Content-Disposition" ,"attachment");
82
82
 
83
83
  OutputStream out = response.getOutputStream();//ここでストリームをつなげる

4

再編集

2018/02/26 14:40

投稿

eisaku123
eisaku123

スコア77

title CHANGED
File without changes
body CHANGED
@@ -12,64 +12,60 @@
12
12
 
13
13
  ```java
14
14
 
15
- //クライアント側 追加
15
+ //クライアント側 再編集 
16
- public class AsyncNetworkTask extends AsyncTask<String, Integer, String> {
16
+ public class AsyncNetworkTask extends AsyncTask<Void, Void, byte[]> {
17
- private TextView txtResult;
17
+
18
- private ProgressBar progress;
19
18
  private ImageView imageview;
20
19
 
21
20
  public AsyncNetworkTask(Context context) {
22
21
  super();
23
22
  MainActivity activity = (MainActivity)context;
24
- txtResult = (TextView)activity.findViewById(R.id.txtResult);
25
- progress = (ProgressBar)activity.findViewById(R.id.progress);
26
- imageview = (ImageView)activity.findViewById(R.id.iv); //追加
23
+ imageview = (ImageView)activity.findViewById(R.id.iv);
27
24
  }
28
25
 
29
26
  @Override
30
- protected String doInBackground(String... params) {
27
+ protected byte[] doInBackground(Void... voids) {
31
- publishProgress(30);
28
+
32
- SystemClock.sleep(1000);
29
+ byte[] data;
33
- publishProgress(60);
30
+
34
31
  StringBuilder builder = new StringBuilder();
35
32
  try {
36
- URL url = new URL(params[0]);
33
+ URL url = new URL("http://192.168.3.5:8080/book777/chapter3");
37
34
  HttpURLConnection con = (HttpURLConnection)url.openConnection();
38
- con.setRequestMethod("GET");
35
+ con.setRequestMethod("GET");
39
-
40
36
  BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
41
37
  String line;
42
38
  while ((line = reader.readLine()) != null){
43
39
  builder.append(line);
44
40
  }
45
-
46
- } catch (IOException e) {
41
+ } catch (IOException e) {
47
42
  e.printStackTrace();
48
43
  }
49
- publishProgress(100);
44
+
50
- return builder.toString();
45
+ String i = builder.toString();
46
+ data=i.getBytes();
47
+ return data;
51
48
  }
52
- ```
53
49
 
50
+ @Override
51
+ protected void onPostExecute(byte[] result) {
52
+ Bitmap bmp2 = null;
54
53
 
54
+ if (result != null) { //byte配列をBitmapオブジェクトに変換する
55
+ bmp2 = BitmapFactory.decodeByteArray(result, 0, result.length);//nullになってしまう
56
+ }
57
+ if (bmp2 == null){ //bmp2がnullかどうか調べる
58
+ imageview.setImageResource(R.drawable.monster);//<----ここに入ってしまう!!!
59
+ }
55
60
 
61
+ imageview.setImageBitmap(bmp2);//nullになってしまうため画像が表示されない
62
+ }
63
+ }
56
- ```java
64
+ ```
57
- //クライアント側
58
- @Override
59
- protected void onPostExecute(String result) {
60
65
 
61
- Bitmap bmp2 = null;
62
66
 
63
- byte[] sbyte = result.getBytes(); //String-->byteに置き換える
64
67
 
65
- if (sbyte != null) {
66
- bmp2 = BitmapFactory.decodeByteArray(sbyte, 0, sbyte.length); //<===ここで戻り値、nullになってしまう!!!
67
- }
68
-
69
- imageview.setImageBitmap(bmp2);
70
- }
71
68
 
72
- ```
73
69
 
74
70
  ```java
75
71
  //サーバー側

3

追加

2018/02/26 13:31

投稿

eisaku123
eisaku123

スコア77

title CHANGED
File without changes
body CHANGED
@@ -11,6 +11,49 @@
11
11
  ちなみにresultからくるデータは以下の以下の以下通り。
12
12
 
13
13
  ```java
14
+
15
+ //クライアント側 追加
16
+ public class AsyncNetworkTask extends AsyncTask<String, Integer, String> {
17
+ private TextView txtResult;
18
+ private ProgressBar progress;
19
+ private ImageView imageview;
20
+
21
+ public AsyncNetworkTask(Context context) {
22
+ super();
23
+ MainActivity activity = (MainActivity)context;
24
+ txtResult = (TextView)activity.findViewById(R.id.txtResult);
25
+ progress = (ProgressBar)activity.findViewById(R.id.progress);
26
+ imageview = (ImageView)activity.findViewById(R.id.iv); //追加
27
+ }
28
+
29
+ @Override
30
+ protected String doInBackground(String... params) {
31
+ publishProgress(30);
32
+ SystemClock.sleep(1000);
33
+ publishProgress(60);
34
+ StringBuilder builder = new StringBuilder();
35
+ try {
36
+ URL url = new URL(params[0]);
37
+ HttpURLConnection con = (HttpURLConnection)url.openConnection();
38
+ con.setRequestMethod("GET");
39
+
40
+ BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
41
+ String line;
42
+ while ((line = reader.readLine()) != null){
43
+ builder.append(line);
44
+ }
45
+
46
+ } catch (IOException e) {
47
+ e.printStackTrace();
48
+ }
49
+ publishProgress(100);
50
+ return builder.toString();
51
+ }
52
+ ```
53
+
54
+
55
+
56
+ ```java
14
57
  //クライアント側
15
58
  @Override
16
59
  protected void onPostExecute(String result) {

2

サーバ側、追記

2018/02/26 09:50

投稿

eisaku123
eisaku123

スコア77

title CHANGED
File without changes
body CHANGED
@@ -5,9 +5,13 @@
5
5
  流れとしては、引数resultからbyteデータに置き換えて、Bitmapに変換したいのですがうまくいきません
6
6
 
7
7
  resultのデータはサーバからくるデータと考えおります。
8
- ちなみにresultからくるデータは以下の以下の通り。
9
8
 
9
+ サーバー側のソースは以下の以下の通り
10
+
11
+ ちなみにresultからくるデータは以下の以下の以下通り。
12
+
10
13
  ```java
14
+ //クライアント側
11
15
  @Override
12
16
  protected void onPostExecute(String result) {
13
17
 
@@ -24,4 +28,43 @@
24
28
 
25
29
  ```
26
30
 
31
+ ```java
32
+ //サーバー側
33
+ @WebServlet(urlPatterns={"/chapter3/*"})
34
+ public class Hello3 extends HttpServlet{
35
+
36
+ @Override
37
+ public void doGet(
38
+ HttpServletRequest request,
39
+ HttpServletResponse response
40
+ ) throws ServletException, IOException {
41
+ response.setContentType("text/html ;charset=UTF-8");
42
+ response.setHeader("Content-Disposition" ,"attachment");
43
+
44
+ OutputStream out = response.getOutputStream();//ここでストリームをつなげる
45
+
46
+ try {
47
+ 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");
48
+
49
+
50
+ int i = input.read();
51
+ while(i !=-1) {
52
+ char c = (char)i;
53
+ out.write(c);//ここでクライアントにデータを流す
54
+ System.out.print(c)
55
+ i = input.read();
56
+ }
57
+ input.close();
58
+ out.close();
59
+
60
+ }catch(Exception e) {
61
+ System.out.println("意味不明のエラーです");
62
+ }catch (Throwable e) {
63
+ System.out.println("byte[] data = IOUtils.toByteArray(input);でエラーです");
64
+ }
65
+ }
66
+
67
+ }
68
+ ```
69
+
27
70
  ������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

スコア77

title CHANGED
File without changes
body CHANGED
@@ -8,6 +8,7 @@
8
8
  ちなみにresultからくるデータは以下の以下の通り。
9
9
 
10
10
  ```java
11
+ @Override
11
12
  protected void onPostExecute(String result) {
12
13
 
13
14
  Bitmap bmp2 = null;