OSDN Git Service

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