OSDN Git Service

画像の詳細情報取得を一括取得から都度取得に変えた。アプリ終了ボタンを追加した。
[gokigen/Gr2Control.git] / app / src / main / java / net / osdn / gokigen / gr2control / camera / utils / SimpleHttpClient.java
1 package net.osdn.gokigen.gr2control.camera.utils;
2
3 import android.graphics.Bitmap;
4 import android.graphics.BitmapFactory;
5 import android.util.Log;
6
7 import java.io.BufferedReader;
8 import java.io.ByteArrayOutputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.InputStreamReader;
12 import java.io.OutputStream;
13 import java.io.OutputStreamWriter;
14 import java.net.HttpURLConnection;
15 import java.net.URL;
16
17 /**
18  *
19  *
20  *
21  */
22 public class SimpleHttpClient
23 {
24     private static final String TAG = SimpleHttpClient.class.getSimpleName();
25     private static final int DEFAULT_TIMEOUT = 10 * 1000; // [ms]
26     private static final int BUFFER_SIZE = 4096;
27
28     public SimpleHttpClient()
29     {
30         Log.v(TAG, "SimpleHttpClient()");
31     }
32
33     /**
34      *
35      *
36      *
37      */
38     public static String httpGet(String url, int timeoutMs)
39     {
40         HttpURLConnection httpConn = null;
41         InputStream inputStream = null;
42         String replyString = "";
43
44         int timeout = timeoutMs;
45         if (timeoutMs < 0)
46         {
47             timeout = DEFAULT_TIMEOUT;
48         }
49
50         //  HTTP GETメソッドで要求を投げる
51         try
52         {
53             final URL urlObj = new URL(url);
54             httpConn = (HttpURLConnection) urlObj.openConnection();
55             httpConn.setRequestMethod("GET");
56             httpConn.setConnectTimeout(timeout);
57             httpConn.setReadTimeout(timeout);
58             httpConn.connect();
59
60             int responseCode = httpConn.getResponseCode();
61             if (responseCode == HttpURLConnection.HTTP_OK)
62             {
63                 inputStream = httpConn.getInputStream();
64             }
65             if (inputStream == null)
66             {
67                 Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
68                 return ("");
69             }
70         }
71         catch (Exception e)
72         {
73             Log.w(TAG, "httpGet: " + url + "  " + e.getMessage());
74             e.printStackTrace();
75             if (httpConn != null)
76             {
77                 httpConn.disconnect();
78             }
79             return ("");
80         }
81
82         // 応答を確認する
83         BufferedReader reader = null;
84         try
85         {
86             StringBuilder responseBuf = new StringBuilder();
87             reader = new BufferedReader(new InputStreamReader(inputStream));
88             int c;
89             while ((c = reader.read()) != -1)
90             {
91                 responseBuf.append((char) c);
92             }
93             replyString = responseBuf.toString();
94         }
95         catch (Exception e)
96         {
97             Log.w(TAG, "httpGet: exception: " + e.getMessage());
98             e.printStackTrace();
99         }
100         finally
101         {
102             try
103             {
104                 if (reader != null)
105                 {
106                     reader.close();
107                 }
108             }
109             catch (Exception e)
110             {
111                 e.printStackTrace();
112             }
113             try
114             {
115                 inputStream.close();
116             }
117             catch (Exception e)
118             {
119                 e.printStackTrace();
120             }
121         }
122         return (replyString);
123     }
124
125     /**
126      *
127      *
128      *
129      */
130     public static byte[] httpGetBytes(String url, int timeoutMs)
131     {
132         HttpURLConnection httpConn = null;
133         InputStream inputStream = null;
134         byte[] receivedData = new byte[0];
135
136         int timeout = timeoutMs;
137         if (timeoutMs < 0)
138         {
139             timeout = DEFAULT_TIMEOUT;
140         }
141
142         //  HTTP GETメソッドで要求を投げる
143         try
144         {
145             final URL urlObj = new URL(url);
146             httpConn = (HttpURLConnection) urlObj.openConnection();
147             httpConn.setRequestMethod("GET");
148             httpConn.setConnectTimeout(timeout);
149             httpConn.setReadTimeout(timeout);
150             httpConn.connect();
151
152             int responseCode = httpConn.getResponseCode();
153             if (responseCode == HttpURLConnection.HTTP_OK)
154             {
155                 inputStream = httpConn.getInputStream();
156             }
157             if (inputStream == null)
158             {
159                 Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
160                 return (receivedData);
161             }
162         }
163         catch (Exception e)
164         {
165             Log.w(TAG, "httpGet: " + url + "  " + e.getMessage());
166             e.printStackTrace();
167             if (httpConn != null)
168             {
169                 httpConn.disconnect();
170             }
171             return (receivedData);
172         }
173
174         // 応答を確認する
175         BufferedReader reader = null;
176         int count = 0;
177         try
178         {
179             byte[] buffer = new byte[BUFFER_SIZE];
180             int c;
181             ByteArrayOutputStream out = new ByteArrayOutputStream();
182             reader = new BufferedReader(new InputStreamReader(inputStream));
183             while ((c = reader.read()) != -1)
184             {
185                 out.write(c);
186                 count++;
187             }
188             receivedData = out.toByteArray();
189             Log.v(TAG, "RECEIVED " + count + " BYTES. ");
190         }
191         catch (Exception e)
192         {
193             Log.w(TAG, "httpGet: exception: " + e.getMessage());
194             e.printStackTrace();
195         }
196         finally
197         {
198             try
199             {
200                 if (reader != null)
201                 {
202                     reader.close();
203                 }
204             }
205             catch (Exception e)
206             {
207                 e.printStackTrace();
208             }
209             try
210             {
211                 inputStream.close();
212             }
213             catch (Exception e)
214             {
215                 e.printStackTrace();
216             }
217         }
218         return (receivedData);
219     }
220     /**
221      *
222      *
223      *
224      */
225     public static Bitmap httpGetBitmap(String url, int timeoutMs)
226     {
227         HttpURLConnection httpConn = null;
228         InputStream inputStream = null;
229         Bitmap bmp = null;
230
231         int timeout = timeoutMs;
232         if (timeoutMs < 0)
233         {
234             timeout = DEFAULT_TIMEOUT;
235         }
236
237         //  HTTP GETメソッドで要求を投げる
238         try
239         {
240             final URL urlObj = new URL(url);
241             httpConn = (HttpURLConnection) urlObj.openConnection();
242             httpConn.setRequestMethod("GET");
243             httpConn.setConnectTimeout(timeout);
244             httpConn.setReadTimeout(timeout);
245             httpConn.connect();
246
247             int responseCode = httpConn.getResponseCode();
248             if (responseCode == HttpURLConnection.HTTP_OK)
249             {
250                 inputStream = httpConn.getInputStream();
251                 if (inputStream != null)
252                 {
253                     bmp = BitmapFactory.decodeStream(inputStream);
254                 }
255             }
256             if (inputStream == null)
257             {
258                 Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
259                 return (null);
260             }
261             inputStream.close();
262         }
263         catch (Exception e)
264         {
265             Log.w(TAG, "httpGet: " + url + "  " + e.getMessage());
266             e.printStackTrace();
267             if (httpConn != null)
268             {
269                 httpConn.disconnect();
270             }
271             return (null);
272         }
273         return (bmp);
274     }
275
276     /**
277      *
278      *
279      *
280      */
281     public static String httpPost(String url, String postData, int timeoutMs)
282     {
283         HttpURLConnection httpConn = null;
284         OutputStream outputStream = null;
285         OutputStreamWriter writer = null;
286         InputStream inputStream = null;
287
288         int timeout = timeoutMs;
289         if (timeoutMs < 0)
290         {
291             timeout = DEFAULT_TIMEOUT;
292         }
293
294         //  HTTP Postメソッドで要求を送出
295         try
296         {
297             final URL urlObj = new URL(url);
298             httpConn = (HttpURLConnection) urlObj.openConnection();
299             httpConn.setRequestMethod("POST");
300             httpConn.setConnectTimeout(timeout);
301             httpConn.setReadTimeout(timeout);
302             httpConn.setDoInput(true);
303             httpConn.setDoOutput(true);
304
305             outputStream = httpConn.getOutputStream();
306             writer = new OutputStreamWriter(outputStream, "UTF-8");
307             writer.write(postData);
308             writer.flush();
309             writer.close();
310             writer = null;
311             outputStream.close();
312             outputStream = null;
313
314             int responseCode = httpConn.getResponseCode();
315             if (responseCode == HttpURLConnection.HTTP_OK)
316             {
317                 inputStream = httpConn.getInputStream();
318             }
319             if (inputStream == null)
320             {
321                 Log.w(TAG, "httpPost: Response Code Error: " + responseCode + ": " + url);
322                 return ("");
323             }
324         }
325         catch (Exception e)
326         {
327             Log.w(TAG, "httpPost: IOException: " + e.getMessage());
328             e.printStackTrace();
329             if (httpConn != null)
330             {
331                 httpConn.disconnect();
332             }
333             return ("");
334         }
335         finally
336         {
337             try
338             {
339                 if (writer != null)
340                 {
341                     writer.close();
342                 }
343             }
344             catch (Exception e)
345             {
346                 e.printStackTrace();
347             }
348             try
349             {
350                 if (outputStream != null)
351                 {
352                     outputStream.close();
353                 }
354             }
355             catch (IOException e)
356             {
357                 e.printStackTrace();
358             }
359         }
360
361         // 応答の読み出し
362         BufferedReader reader = null;
363         String replyString = "";
364         try
365         {
366             StringBuilder responseBuf = new StringBuilder();
367             reader = new BufferedReader(new InputStreamReader(inputStream));
368
369             int c;
370             while ((c = reader.read()) != -1)
371             {
372                 responseBuf.append((char) c);
373             }
374             replyString = responseBuf.toString();
375         }
376         catch (Exception e)
377         {
378             e.printStackTrace();
379         }
380         finally
381         {
382             try
383             {
384                 if (reader != null)
385                 {
386                     reader.close();
387                 }
388             }
389             catch (IOException e)
390             {
391                 e.printStackTrace();
392             }
393         }
394         return (replyString);
395     }
396 }