1 package net.osdn.gokigen.gr2control.camera.utils;
3 import android.graphics.Bitmap;
4 import android.graphics.BitmapFactory;
5 import android.support.annotation.NonNull;
6 import android.util.Log;
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;
24 public class SimpleHttpClient
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;
30 public SimpleHttpClient()
32 Log.v(TAG, "SimpleHttpClient()");
40 public static String httpGet(String url, int timeoutMs)
42 HttpURLConnection httpConn = null;
43 InputStream inputStream = null;
44 String replyString = "";
46 int timeout = timeoutMs;
49 timeout = DEFAULT_TIMEOUT;
52 // HTTP GETメソッドで要求を投げる
55 final URL urlObj = new URL(url);
56 httpConn = (HttpURLConnection) urlObj.openConnection();
57 httpConn.setRequestMethod("GET");
58 httpConn.setConnectTimeout(timeout);
59 httpConn.setReadTimeout(timeout);
62 int responseCode = httpConn.getResponseCode();
63 if (responseCode == HttpURLConnection.HTTP_OK)
65 inputStream = httpConn.getInputStream();
67 if (inputStream == null)
69 Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
75 Log.w(TAG, "httpGet: " + url + " " + e.getMessage());
79 httpConn.disconnect();
85 BufferedReader reader = null;
88 StringBuilder responseBuf = new StringBuilder();
89 reader = new BufferedReader(new InputStreamReader(inputStream));
91 while ((c = reader.read()) != -1)
93 responseBuf.append((char) c);
95 replyString = responseBuf.toString();
99 Log.w(TAG, "httpGet: exception: " + e.getMessage());
124 return (replyString);
132 public static void httpGetBytes(String url, int timeoutMs, @NonNull IReceivedMessageCallback callback)
134 HttpURLConnection httpConn = null;
135 InputStream inputStream = null;
136 int timeout = timeoutMs;
139 timeout = DEFAULT_TIMEOUT;
142 // HTTP GETメソッドで要求を投げる
145 final URL urlObj = new URL(url);
146 httpConn = (HttpURLConnection) urlObj.openConnection();
147 httpConn.setRequestMethod("GET");
148 httpConn.setConnectTimeout(timeout);
149 httpConn.setReadTimeout(timeout);
152 int responseCode = httpConn.getResponseCode();
153 if (responseCode == HttpURLConnection.HTTP_OK)
155 inputStream = httpConn.getInputStream();
157 if (inputStream == null)
159 Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
160 callback.onErrorOccurred(new NullPointerException());
161 callback.onCompleted();
167 Log.w(TAG, "httpGet: " + url + " " + e.getMessage());
169 if (httpConn != null)
171 httpConn.disconnect();
173 callback.onErrorOccurred(e);
174 callback.onCompleted();
181 int contentLength = httpConn.getContentLength();
182 byte[] buffer = new byte[BUFFER_SIZE];
183 BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
185 int readSize = bufferedInputStream.read(buffer, 0, BUFFER_SIZE);
186 while (readSize != -1)
188 callback.onReceive(readBytes, contentLength, buffer);
189 readBytes += readSize;
190 readSize = bufferedInputStream.read(buffer, 0, BUFFER_SIZE);
192 Log.v(TAG, "RECEIVED " + readBytes + " BYTES. (contentLength : " + contentLength + ")");
193 bufferedInputStream.close();
197 Log.w(TAG, "httpGet: exception: " + e.getMessage());
199 callback.onErrorOccurred(e);
212 callback.onCompleted();
220 public static Bitmap httpGetBitmap(String url, int timeoutMs)
222 HttpURLConnection httpConn = null;
223 InputStream inputStream = null;
226 int timeout = timeoutMs;
229 timeout = DEFAULT_TIMEOUT;
232 // HTTP GETメソッドで要求を投げる
235 final URL urlObj = new URL(url);
236 httpConn = (HttpURLConnection) urlObj.openConnection();
237 httpConn.setRequestMethod("GET");
238 httpConn.setConnectTimeout(timeout);
239 httpConn.setReadTimeout(timeout);
242 int responseCode = httpConn.getResponseCode();
243 if (responseCode == HttpURLConnection.HTTP_OK)
245 inputStream = httpConn.getInputStream();
246 if (inputStream != null)
248 bmp = BitmapFactory.decodeStream(inputStream);
251 if (inputStream == null)
253 Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
260 Log.w(TAG, "httpGet: " + url + " " + e.getMessage());
262 if (httpConn != null)
264 httpConn.disconnect();
276 public static String httpPost(String url, String postData, int timeoutMs)
278 HttpURLConnection httpConn = null;
279 OutputStream outputStream = null;
280 OutputStreamWriter writer = null;
281 InputStream inputStream = null;
283 int timeout = timeoutMs;
286 timeout = DEFAULT_TIMEOUT;
289 // HTTP Postメソッドで要求を送出
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);
300 outputStream = httpConn.getOutputStream();
301 writer = new OutputStreamWriter(outputStream, "UTF-8");
302 writer.write(postData);
306 outputStream.close();
309 int responseCode = httpConn.getResponseCode();
310 if (responseCode == HttpURLConnection.HTTP_OK)
312 inputStream = httpConn.getInputStream();
314 if (inputStream == null)
316 Log.w(TAG, "httpPost: Response Code Error: " + responseCode + ": " + url);
322 Log.w(TAG, "httpPost: IOException: " + e.getMessage());
324 if (httpConn != null)
326 httpConn.disconnect();
345 if (outputStream != null)
347 outputStream.close();
350 catch (IOException e)
357 BufferedReader reader = null;
358 String replyString = "";
361 StringBuilder responseBuf = new StringBuilder();
362 reader = new BufferedReader(new InputStreamReader(inputStream));
365 while ((c = reader.read()) != -1)
367 responseBuf.append((char) c);
369 replyString = responseBuf.toString();
384 catch (IOException e)
389 return (replyString);
392 public interface IReceivedMessageCallback
395 void onErrorOccurred(Exception e);
396 void onReceive(int readBytes, int length, byte[] data);