1 package net.osdn.gokigen.pkremote.camera.utils;
3 import android.graphics.Bitmap;
4 import android.graphics.BitmapFactory;
5 import android.util.Log;
7 import java.io.BufferedReader;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11 import java.io.OutputStream;
12 import java.io.OutputStreamWriter;
13 import java.net.HttpURLConnection;
15 import java.util.List;
18 import androidx.annotation.NonNull;
19 import androidx.annotation.Nullable;
26 public class SimpleHttpClient
28 private static final String TAG = SimpleHttpClient.class.getSimpleName();
29 private static final int DEFAULT_TIMEOUT = 10 * 1000; // [ms]
30 private static final int BUFFER_SIZE = 131072 * 2; // 256kB
32 public SimpleHttpClient()
34 Log.v(TAG, "SimpleHttpClient()");
42 public static String httpGet(String url, int timeoutMs)
44 HttpURLConnection httpConn = null;
45 InputStream inputStream = null;
46 String replyString = "";
48 int timeout = timeoutMs;
51 timeout = DEFAULT_TIMEOUT;
54 // HTTP GETメソッドで要求を投げる
57 final URL urlObj = new URL(url);
58 httpConn = (HttpURLConnection) urlObj.openConnection();
59 httpConn.setRequestMethod("GET");
60 httpConn.setConnectTimeout(timeout);
61 httpConn.setReadTimeout(timeout);
64 int responseCode = httpConn.getResponseCode();
65 if (responseCode == HttpURLConnection.HTTP_OK)
67 inputStream = httpConn.getInputStream();
69 if (inputStream == null)
71 Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
77 Log.w(TAG, "httpGet: " + url + " " + e.getMessage());
81 httpConn.disconnect();
87 BufferedReader reader = null;
90 StringBuilder responseBuf = new StringBuilder();
91 reader = new BufferedReader(new InputStreamReader(inputStream));
93 while ((c = reader.read()) != -1)
95 responseBuf.append((char) c);
97 replyString = responseBuf.toString();
101 Log.w(TAG, "httpGet: exception: " + e.getMessage());
126 return (replyString);
134 public static void httpGetBytes(String url, Map<String, String> setProperty, int timeoutMs, @NonNull IReceivedMessageCallback callback)
136 httpCommandBytes(url, "GET", null, setProperty, null, timeoutMs, callback);
144 public static void httpPostBytes(String url, String postData, Map<String, String> setProperty, int timeoutMs, @NonNull IReceivedMessageCallback callback)
146 httpCommandBytes(url, "POST", postData, setProperty, null, timeoutMs, callback);
154 public static void httpPostBytesWithHeader(String url, String postData, Map<String, String> setProperty, @Nullable String contentType, int timeoutMs, @NonNull IReceivedMessageCallback callback)
156 httpCommandBytes(url, "POST", postData, setProperty, contentType, timeoutMs, callback);
159 private static void httpCommandBytes(@NonNull String url, @NonNull String requestMethod, @Nullable String postData, @Nullable Map<String, String> setProperty, @Nullable String contentType, int timeoutMs, @NonNull IReceivedMessageCallback callback)
161 HttpURLConnection httpConn = null;
162 OutputStream outputStream = null;
163 OutputStreamWriter writer = null;
164 InputStream inputStream = null;
165 int timeout = timeoutMs;
168 timeout = DEFAULT_TIMEOUT;
174 final URL urlObj = new URL(url);
175 httpConn = (HttpURLConnection) urlObj.openConnection();
176 httpConn.setRequestMethod(requestMethod);
177 if (setProperty != null)
179 for (String key : setProperty.keySet())
181 String value = setProperty.get(key);
182 httpConn.setRequestProperty(key, value);
185 if (contentType != null)
187 httpConn.setRequestProperty("Content-Type", contentType);
189 httpConn.setConnectTimeout(timeout);
190 httpConn.setReadTimeout(timeout);
191 if (postData == null)
197 httpConn.setDoInput(true);
198 httpConn.setDoOutput(true);
199 outputStream = httpConn.getOutputStream();
200 writer = new OutputStreamWriter(outputStream, "UTF-8");
201 writer.write(postData);
205 outputStream.close();
209 int responseCode = httpConn.getResponseCode();
210 if (responseCode == HttpURLConnection.HTTP_OK)
212 inputStream = httpConn.getInputStream();
214 if (inputStream == null)
216 Log.w(TAG, " http " + requestMethod + " Response Code Error: " + responseCode + ": " + url);
217 callback.onErrorOccurred(new NullPointerException());
218 callback.onCompleted();
224 Log.w(TAG, "http " + requestMethod + " " + url + " " + e.getMessage());
226 if (httpConn != null)
228 httpConn.disconnect();
230 callback.onErrorOccurred(e);
231 callback.onCompleted();
249 if (outputStream != null)
251 outputStream.close();
254 catch (IOException e)
263 int contentLength = httpConn.getContentLength();
264 if (contentLength < 0)
266 // コンテンツ長が取れない場合の処理...
269 Map<String, List<String>> headers = httpConn.getHeaderFields();
272 // 応答ヘッダをすべてダンプするロジック...
273 for (String key : headers.keySet())
275 final List<String> valueList = headers.get(key);
276 Log.v(TAG, " " + key + " : " + getValue(valueList));
280 // コンテンツ長さが取れない場合は、HTTP応答ヘッダから取得する
281 List<String> valueList = headers.get("X-FILE_SIZE");
284 if (valueList != null)
286 contentLength = Integer.parseInt(getValue(valueList));
291 ee.printStackTrace();
300 byte[] buffer = new byte[BUFFER_SIZE];
302 int readSize = inputStream.read(buffer, 0, BUFFER_SIZE);
303 while (readSize != -1)
305 callback.onReceive(readBytes, contentLength, readSize, buffer);
306 readBytes += readSize;
307 readSize = inputStream.read(buffer, 0, BUFFER_SIZE);
309 Log.v(TAG, "RECEIVED " + readBytes + " BYTES. (contentLength : " + contentLength + ")");
314 Log.w(TAG, "httpGet: exception: " + e.getMessage());
316 callback.onErrorOccurred(e);
329 callback.onCompleted();
333 public static String getValue(List<String> valueList)
336 boolean isFirst = true;
337 final StringBuilder values = new StringBuilder();
338 for (String value : valueList)
340 values.append(value);
350 return (values.toString());
353 public static Bitmap httpGetBitmap(String url, Map<String, String> setProperty, int timeoutMs)
355 return (httpCommandBitmap(url, "GET", null, setProperty, null, timeoutMs));
363 public static Bitmap httpPostBitmap(String url, String postData, int timeoutMs)
365 return (httpCommandBitmap(url, "POST", postData, null, null, timeoutMs));
373 public static Bitmap httpPostBitmapWithHeader(String url, String postData, @Nullable Map<String, String> setProperty, @Nullable String contentType, int timeoutMs)
375 return (httpCommandBitmap(url, "POST", postData, setProperty, contentType, timeoutMs));
383 private static Bitmap httpCommandBitmap(@NonNull String url, @NonNull String requestMethod, @Nullable String postData, @Nullable Map<String, String> setProperty, @Nullable String contentType, int timeoutMs)
385 HttpURLConnection httpConn = null;
386 InputStream inputStream = null;
387 OutputStream outputStream = null;
388 OutputStreamWriter writer = null;
391 int timeout = timeoutMs;
394 timeout = DEFAULT_TIMEOUT;
400 final URL urlObj = new URL(url);
401 httpConn = (HttpURLConnection) urlObj.openConnection();
402 httpConn.setRequestMethod(requestMethod);
403 if (setProperty != null)
405 for (String key : setProperty.keySet())
407 String value = setProperty.get(key);
408 httpConn.setRequestProperty(key, value);
411 if (contentType != null)
413 httpConn.setRequestProperty("Content-Type", contentType);
415 httpConn.setConnectTimeout(timeout);
416 httpConn.setReadTimeout(timeout);
418 if (postData == null)
424 httpConn.setDoInput(true);
425 httpConn.setDoOutput(true);
426 outputStream = httpConn.getOutputStream();
427 writer = new OutputStreamWriter(outputStream, "UTF-8");
428 writer.write(postData);
432 outputStream.close();
436 int responseCode = httpConn.getResponseCode();
437 if (responseCode == HttpURLConnection.HTTP_OK)
439 inputStream = httpConn.getInputStream();
440 if (inputStream != null)
442 bmp = BitmapFactory.decodeStream(inputStream);
445 if (inputStream == null)
447 Log.w(TAG, "http: (" + requestMethod + ") Response Code Error: " + responseCode + ": " + url);
454 Log.w(TAG, "http: (" + requestMethod + ") " + url + " " + e.getMessage());
456 if (httpConn != null)
458 httpConn.disconnect();
477 if (outputStream != null)
479 outputStream.close();
482 catch (IOException e)
495 public static String httpPost(String url, String postData, int timeoutMs)
497 return (httpCommand(url, "POST", postData, null, null, timeoutMs));
505 public static String httpGetWithHeader(String url, Map<String, String> headerMap, String contentType, int timeoutMs)
507 return (httpCommand(url, "GET", null, headerMap, contentType, timeoutMs));
515 public static String httpPostWithHeader(String url, String postData, Map<String, String> headerMap, String contentType, int timeoutMs)
517 return (httpCommand(url, "POST", postData, headerMap, contentType, timeoutMs));
525 public static String httpPutWithHeader(String url, String putData, Map<String, String> headerMap, String contentType, int timeoutMs)
527 return (httpCommand(url, "PUT", putData, headerMap, contentType, timeoutMs));
535 public static String httpPut(String url, String postData, int timeoutMs)
537 return (httpCommand(url, "PUT", postData, null, null, timeoutMs));
545 public static String httpOptions(String url, String optionsData, int timeoutMs)
547 return (httpCommand(url, "OPTIONS", optionsData, null, null, timeoutMs));
555 private static String httpCommand(String url, String requestMethod, String postData, Map<String, String> setProperty, String contentType, int timeoutMs)
557 HttpURLConnection httpConn = null;
558 OutputStream outputStream = null;
559 OutputStreamWriter writer = null;
560 InputStream inputStream = null;
562 int timeout = timeoutMs;
565 timeout = DEFAULT_TIMEOUT;
571 final URL urlObj = new URL(url);
572 httpConn = (HttpURLConnection) urlObj.openConnection();
573 httpConn.setRequestMethod(requestMethod);
574 if (setProperty != null)
576 for (String key : setProperty.keySet())
578 String value = setProperty.get(key);
579 httpConn.setRequestProperty(key, value);
582 if (contentType != null)
584 httpConn.setRequestProperty("Content-Type", contentType);
586 httpConn.setConnectTimeout(timeout);
587 httpConn.setReadTimeout(timeout);
589 if (postData == null)
595 httpConn.setDoInput(true);
596 httpConn.setDoOutput(true);
597 outputStream = httpConn.getOutputStream();
598 writer = new OutputStreamWriter(outputStream, "UTF-8");
599 writer.write(postData);
603 outputStream.close();
606 int responseCode = httpConn.getResponseCode();
607 if (responseCode == HttpURLConnection.HTTP_OK)
609 inputStream = httpConn.getInputStream();
611 if (inputStream == null)
613 Log.w(TAG, "http " + requestMethod + " : Response Code Error: " + responseCode + ": " + url);
615 inputStream = httpConn.getInputStream();
616 if (inputStream != null)
618 // DUMP RESPONSE DETAIL
619 Log.w(TAG, " RESP : " + readFromInputStream(inputStream));
627 Log.w(TAG, "http " + requestMethod + " : IOException: " + e.getMessage());
629 if (httpConn != null)
631 httpConn.disconnect();
650 if (outputStream != null)
652 outputStream.close();
655 catch (IOException e)
662 return (readFromInputStream(inputStream));
664 BufferedReader reader = null;
665 String replyString = "";
668 StringBuilder responseBuf = new StringBuilder();
669 reader = new BufferedReader(new InputStreamReader(inputStream));
672 while ((c = reader.read()) != -1)
674 responseBuf.append((char) c);
676 replyString = responseBuf.toString();
691 catch (IOException e)
696 return (replyString);
700 private static String readFromInputStream(InputStream inputStream)
702 BufferedReader reader = null;
703 String replyString = "";
704 if (inputStream == null)
710 StringBuilder responseBuf = new StringBuilder();
711 reader = new BufferedReader(new InputStreamReader(inputStream));
714 while ((c = reader.read()) != -1)
716 responseBuf.append((char) c);
718 replyString = responseBuf.toString();
733 catch (IOException e)
738 return (replyString);
742 public interface IReceivedMessageCallback
745 void onErrorOccurred(Exception e);
746 void onReceive(int readBytes, int length, int size, byte[] data);