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.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;
22 public class SimpleHttpClient
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;
28 public SimpleHttpClient()
30 Log.v(TAG, "SimpleHttpClient()");
38 public static String httpGet(String url, int timeoutMs)
40 HttpURLConnection httpConn = null;
41 InputStream inputStream = null;
42 String replyString = "";
44 int timeout = timeoutMs;
47 timeout = DEFAULT_TIMEOUT;
50 // HTTP GETメソッドで要求を投げる
53 final URL urlObj = new URL(url);
54 httpConn = (HttpURLConnection) urlObj.openConnection();
55 httpConn.setRequestMethod("GET");
56 httpConn.setConnectTimeout(timeout);
57 httpConn.setReadTimeout(timeout);
60 int responseCode = httpConn.getResponseCode();
61 if (responseCode == HttpURLConnection.HTTP_OK)
63 inputStream = httpConn.getInputStream();
65 if (inputStream == null)
67 Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
73 Log.w(TAG, "httpGet: " + url + " " + e.getMessage());
77 httpConn.disconnect();
83 BufferedReader reader = null;
86 StringBuilder responseBuf = new StringBuilder();
87 reader = new BufferedReader(new InputStreamReader(inputStream));
89 while ((c = reader.read()) != -1)
91 responseBuf.append((char) c);
93 replyString = responseBuf.toString();
97 Log.w(TAG, "httpGet: exception: " + e.getMessage());
122 return (replyString);
130 public static void httpGetBytes(String url, int timeoutMs, @NonNull IReceivedMessageCallback callback)
132 HttpURLConnection httpConn = null;
133 InputStream inputStream = null;
134 int timeout = timeoutMs;
137 timeout = DEFAULT_TIMEOUT;
140 // HTTP GETメソッドで要求を投げる
143 final URL urlObj = new URL(url);
144 httpConn = (HttpURLConnection) urlObj.openConnection();
145 httpConn.setRequestMethod("GET");
146 httpConn.setConnectTimeout(timeout);
147 httpConn.setReadTimeout(timeout);
150 int responseCode = httpConn.getResponseCode();
151 if (responseCode == HttpURLConnection.HTTP_OK)
153 inputStream = httpConn.getInputStream();
155 if (inputStream == null)
157 Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
158 callback.onErrorOccurred(new NullPointerException());
159 callback.onCompleted();
165 Log.w(TAG, "httpGet: " + url + " " + e.getMessage());
167 if (httpConn != null)
169 httpConn.disconnect();
171 callback.onErrorOccurred(e);
172 callback.onCompleted();
179 int contentLength = httpConn.getContentLength();
180 byte[] buffer = new byte[BUFFER_SIZE];
182 int readSize = inputStream.read(buffer, 0, BUFFER_SIZE);
183 while (readSize != -1)
185 callback.onReceive(readBytes, contentLength, readSize, buffer);
186 readBytes += readSize;
187 readSize = inputStream.read(buffer, 0, BUFFER_SIZE);
189 Log.v(TAG, "RECEIVED " + readBytes + " BYTES. (contentLength : " + contentLength + ")");
194 Log.w(TAG, "httpGet: exception: " + e.getMessage());
196 callback.onErrorOccurred(e);
209 callback.onCompleted();
217 public static Bitmap httpGetBitmap(String url, int timeoutMs)
219 HttpURLConnection httpConn = null;
220 InputStream inputStream = null;
223 int timeout = timeoutMs;
226 timeout = DEFAULT_TIMEOUT;
229 // HTTP GETメソッドで要求を投げる
232 final URL urlObj = new URL(url);
233 httpConn = (HttpURLConnection) urlObj.openConnection();
234 httpConn.setRequestMethod("GET");
235 httpConn.setConnectTimeout(timeout);
236 httpConn.setReadTimeout(timeout);
239 int responseCode = httpConn.getResponseCode();
240 if (responseCode == HttpURLConnection.HTTP_OK)
242 inputStream = httpConn.getInputStream();
243 if (inputStream != null)
245 bmp = BitmapFactory.decodeStream(inputStream);
248 if (inputStream == null)
250 Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
257 Log.w(TAG, "httpGet: " + url + " " + e.getMessage());
259 if (httpConn != null)
261 httpConn.disconnect();
273 public static String httpPost(String url, String postData, int timeoutMs)
275 return (httpCommand(url, "POST", postData, timeoutMs));
283 public static String httpPut(String url, String postData, int timeoutMs)
285 return (httpCommand(url, "PUT", postData, timeoutMs));
293 private static String httpCommand(String url, String requestMethod, String postData, int timeoutMs)
295 HttpURLConnection httpConn = null;
296 OutputStream outputStream = null;
297 OutputStreamWriter writer = null;
298 InputStream inputStream = null;
300 int timeout = timeoutMs;
303 timeout = DEFAULT_TIMEOUT;
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);
317 outputStream = httpConn.getOutputStream();
318 writer = new OutputStreamWriter(outputStream, "UTF-8");
319 writer.write(postData);
323 outputStream.close();
326 int responseCode = httpConn.getResponseCode();
327 if (responseCode == HttpURLConnection.HTTP_OK)
329 inputStream = httpConn.getInputStream();
331 if (inputStream == null)
333 Log.w(TAG, "http " + requestMethod + " : Response Code Error: " + responseCode + ": " + url);
339 Log.w(TAG, "http " + requestMethod + " : IOException: " + e.getMessage());
341 if (httpConn != null)
343 httpConn.disconnect();
362 if (outputStream != null)
364 outputStream.close();
367 catch (IOException e)
374 BufferedReader reader = null;
375 String replyString = "";
378 StringBuilder responseBuf = new StringBuilder();
379 reader = new BufferedReader(new InputStreamReader(inputStream));
382 while ((c = reader.read()) != -1)
384 responseBuf.append((char) c);
386 replyString = responseBuf.toString();
401 catch (IOException e)
406 return (replyString);
409 public interface IReceivedMessageCallback
412 void onErrorOccurred(Exception e);
413 void onReceive(int readBytes, int length, int size, byte[] data);