OSDN Git Service

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