OSDN Git Service

1b08370a13bf61f005b0c1ca373fdf452af5b853
[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.BufferedReader;
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 void httpGetBytes(String url, int timeoutMs, @NonNull IReceivedMessageCallback callback)
131     {
132         HttpURLConnection httpConn = null;
133         InputStream inputStream = null;
134         int timeout = timeoutMs;
135         if (timeoutMs < 0)
136         {
137             timeout = DEFAULT_TIMEOUT;
138         }
139
140         //  HTTP GETメソッドで要求を投げる
141         try
142         {
143             final URL urlObj = new URL(url);
144             httpConn = (HttpURLConnection) urlObj.openConnection();
145             httpConn.setRequestMethod("GET");
146             httpConn.setConnectTimeout(timeout);
147             httpConn.setReadTimeout(timeout);
148             httpConn.connect();
149
150             int responseCode = httpConn.getResponseCode();
151             if (responseCode == HttpURLConnection.HTTP_OK)
152             {
153                 inputStream = httpConn.getInputStream();
154             }
155             if (inputStream == null)
156             {
157                 Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
158                 callback.onErrorOccurred(new NullPointerException());
159                 callback.onCompleted();
160                 return;
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             callback.onErrorOccurred(e);
172             callback.onCompleted();
173             return;
174         }
175
176         // 応答を確認する
177         try
178         {
179             int contentLength = httpConn.getContentLength();
180             byte[] buffer = new byte[BUFFER_SIZE];
181             int readBytes = 0;
182             int readSize = inputStream.read(buffer, 0, BUFFER_SIZE);
183             while (readSize != -1)
184             {
185                 callback.onReceive(readBytes, contentLength, readSize, buffer);
186                 readBytes += readSize;
187                 readSize = inputStream.read(buffer, 0, BUFFER_SIZE);
188             }
189             Log.v(TAG, "RECEIVED " + readBytes + " BYTES. (contentLength : " + contentLength + ")");
190             inputStream.close();
191         }
192         catch (Exception e)
193         {
194             Log.w(TAG, "httpGet: exception: " + e.getMessage());
195             e.printStackTrace();
196             callback.onErrorOccurred(e);
197         }
198         finally
199         {
200             try
201             {
202                 inputStream.close();
203             }
204             catch (Exception e)
205             {
206                 e.printStackTrace();
207             }
208         }
209         callback.onCompleted();
210     }
211
212     /**
213      *
214      *
215      *
216      */
217     public static Bitmap httpGetBitmap(String url, int timeoutMs)
218     {
219         HttpURLConnection httpConn = null;
220         InputStream inputStream = null;
221         Bitmap bmp = null;
222
223         int timeout = timeoutMs;
224         if (timeoutMs < 0)
225         {
226             timeout = DEFAULT_TIMEOUT;
227         }
228
229         //  HTTP GETメソッドで要求を投げる
230         try
231         {
232             final URL urlObj = new URL(url);
233             httpConn = (HttpURLConnection) urlObj.openConnection();
234             httpConn.setRequestMethod("GET");
235             httpConn.setConnectTimeout(timeout);
236             httpConn.setReadTimeout(timeout);
237             httpConn.connect();
238
239             int responseCode = httpConn.getResponseCode();
240             if (responseCode == HttpURLConnection.HTTP_OK)
241             {
242                 inputStream = httpConn.getInputStream();
243                 if (inputStream != null)
244                 {
245                     bmp = BitmapFactory.decodeStream(inputStream);
246                 }
247             }
248             if (inputStream == null)
249             {
250                 Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
251                 return (null);
252             }
253             inputStream.close();
254         }
255         catch (Exception e)
256         {
257             Log.w(TAG, "httpGet: " + url + "  " + e.getMessage());
258             e.printStackTrace();
259             if (httpConn != null)
260             {
261                 httpConn.disconnect();
262             }
263             return (null);
264         }
265         return (bmp);
266     }
267
268     /**
269      *
270      *
271      *
272      */
273     public static String httpPost(String url, String postData, int timeoutMs)
274     {
275         return (httpCommand(url, "POST", postData, timeoutMs));
276     }
277
278     /**
279      *
280      *
281      *
282      */
283     public static String httpPut(String url, String postData, int timeoutMs)
284     {
285         return (httpCommand(url, "PUT", postData, timeoutMs));
286     }
287
288     /**
289      *
290      *
291      *
292      */
293     private static String httpCommand(String url, String requestMethod, String postData, int timeoutMs)
294     {
295         HttpURLConnection httpConn = null;
296         OutputStream outputStream = null;
297         OutputStreamWriter writer = null;
298         InputStream inputStream = null;
299
300         int timeout = timeoutMs;
301         if (timeoutMs < 0)
302         {
303             timeout = DEFAULT_TIMEOUT;
304         }
305
306         //  HTTP メソッドで要求を送出
307         try
308         {
309             final URL urlObj = new URL(url);
310             httpConn = (HttpURLConnection) urlObj.openConnection();
311             httpConn.setRequestMethod(requestMethod);
312             httpConn.setConnectTimeout(timeout);
313             httpConn.setReadTimeout(timeout);
314             httpConn.setDoInput(true);
315             httpConn.setDoOutput(true);
316
317             outputStream = httpConn.getOutputStream();
318             writer = new OutputStreamWriter(outputStream, "UTF-8");
319             writer.write(postData);
320             writer.flush();
321             writer.close();
322             writer = null;
323             outputStream.close();
324             outputStream = null;
325
326             int responseCode = httpConn.getResponseCode();
327             if (responseCode == HttpURLConnection.HTTP_OK)
328             {
329                 inputStream = httpConn.getInputStream();
330             }
331             if (inputStream == null)
332             {
333                 Log.w(TAG, "http " + requestMethod + " : Response Code Error: " + responseCode + ": " + url);
334                 return ("");
335             }
336         }
337         catch (Exception e)
338         {
339             Log.w(TAG, "http " + requestMethod + " : IOException: " + e.getMessage());
340             e.printStackTrace();
341             if (httpConn != null)
342             {
343                 httpConn.disconnect();
344             }
345             return ("");
346         }
347         finally
348         {
349             try
350             {
351                 if (writer != null)
352                 {
353                     writer.close();
354                 }
355             }
356             catch (Exception e)
357             {
358                 e.printStackTrace();
359             }
360             try
361             {
362                 if (outputStream != null)
363                 {
364                     outputStream.close();
365                 }
366             }
367             catch (IOException e)
368             {
369                 e.printStackTrace();
370             }
371         }
372
373         // 応答の読み出し
374         BufferedReader reader = null;
375         String replyString = "";
376         try
377         {
378             StringBuilder responseBuf = new StringBuilder();
379             reader = new BufferedReader(new InputStreamReader(inputStream));
380
381             int c;
382             while ((c = reader.read()) != -1)
383             {
384                 responseBuf.append((char) c);
385             }
386             replyString = responseBuf.toString();
387         }
388         catch (Exception e)
389         {
390             e.printStackTrace();
391         }
392         finally
393         {
394             try
395             {
396                 if (reader != null)
397                 {
398                     reader.close();
399                 }
400             }
401             catch (IOException e)
402             {
403                 e.printStackTrace();
404             }
405         }
406         return (replyString);
407     }
408
409     public interface IReceivedMessageCallback
410     {
411         void onCompleted();
412         void onErrorOccurred(Exception  e);
413         void onReceive(int readBytes, int length, int size, byte[] data);
414     }
415 }