1 package net.osdn.gokigen.gr2control.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.ByteArrayOutputStream;
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]
27 public SimpleHttpClient()
29 Log.v(TAG, "SimpleHttpClient()");
37 public static String httpGet(String url, int timeoutMs)
39 HttpURLConnection httpConn = null;
40 InputStream inputStream = null;
41 String replyString = "";
43 int timeout = timeoutMs;
46 timeout = DEFAULT_TIMEOUT;
49 // HTTP GETメソッドで要求を投げる
52 final URL urlObj = new URL(url);
53 httpConn = (HttpURLConnection) urlObj.openConnection();
54 httpConn.setRequestMethod("GET");
55 httpConn.setConnectTimeout(timeout);
56 httpConn.setReadTimeout(timeout);
59 int responseCode = httpConn.getResponseCode();
60 if (responseCode == HttpURLConnection.HTTP_OK)
62 inputStream = httpConn.getInputStream();
64 if (inputStream == null)
66 Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
72 Log.w(TAG, "httpGet: " + url + " " + e.getMessage());
76 httpConn.disconnect();
82 BufferedReader reader = null;
85 StringBuilder responseBuf = new StringBuilder();
86 reader = new BufferedReader(new InputStreamReader(inputStream));
88 while ((c = reader.read()) != -1)
90 responseBuf.append((char) c);
92 replyString = responseBuf.toString();
96 Log.w(TAG, "httpGet: exception: " + e.getMessage());
121 return (replyString);
129 public static byte[] httpGetBytes(String url, int timeoutMs)
131 HttpURLConnection httpConn = null;
132 InputStream inputStream = null;
133 byte[] receivedData = new byte[0];
135 int timeout = timeoutMs;
138 timeout = DEFAULT_TIMEOUT;
141 // HTTP GETメソッドで要求を投げる
144 final URL urlObj = new URL(url);
145 httpConn = (HttpURLConnection) urlObj.openConnection();
146 httpConn.setRequestMethod("GET");
147 httpConn.setConnectTimeout(timeout);
148 httpConn.setReadTimeout(timeout);
151 int responseCode = httpConn.getResponseCode();
152 if (responseCode == HttpURLConnection.HTTP_OK)
154 inputStream = httpConn.getInputStream();
156 if (inputStream == null)
158 Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
159 return (receivedData);
164 Log.w(TAG, "httpGet: " + url + " " + e.getMessage());
166 if (httpConn != null)
168 httpConn.disconnect();
170 return (receivedData);
174 BufferedReader reader = null;
178 ByteArrayOutputStream out = new ByteArrayOutputStream();
179 reader = new BufferedReader(new InputStreamReader(inputStream));
181 while ((c = reader.read()) != -1)
186 receivedData = out.toByteArray();
187 Log.v(TAG, "RECEIVED " + count + " BYTES. ");
191 Log.w(TAG, "httpGet: exception: " + e.getMessage());
216 return (receivedData);
223 public static Bitmap httpGetBitmap(String url, int timeoutMs)
225 HttpURLConnection httpConn = null;
226 InputStream inputStream = null;
229 int timeout = timeoutMs;
232 timeout = DEFAULT_TIMEOUT;
235 // HTTP GETメソッドで要求を投げる
238 final URL urlObj = new URL(url);
239 httpConn = (HttpURLConnection) urlObj.openConnection();
240 httpConn.setRequestMethod("GET");
241 httpConn.setConnectTimeout(timeout);
242 httpConn.setReadTimeout(timeout);
245 int responseCode = httpConn.getResponseCode();
246 if (responseCode == HttpURLConnection.HTTP_OK)
248 inputStream = httpConn.getInputStream();
249 if (inputStream != null)
251 bmp = BitmapFactory.decodeStream(inputStream);
254 if (inputStream == null)
256 Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
263 Log.w(TAG, "httpGet: " + url + " " + e.getMessage());
265 if (httpConn != null)
267 httpConn.disconnect();
279 public static String httpPost(String url, String postData, int timeoutMs)
281 HttpURLConnection httpConn = null;
282 OutputStream outputStream = null;
283 OutputStreamWriter writer = null;
284 InputStream inputStream = null;
286 int timeout = timeoutMs;
289 timeout = DEFAULT_TIMEOUT;
292 // HTTP Postメソッドで要求を送出
295 final URL urlObj = new URL(url);
296 httpConn = (HttpURLConnection) urlObj.openConnection();
297 httpConn.setRequestMethod("POST");
298 httpConn.setConnectTimeout(timeout);
299 httpConn.setReadTimeout(timeout);
300 httpConn.setDoInput(true);
301 httpConn.setDoOutput(true);
303 outputStream = httpConn.getOutputStream();
304 writer = new OutputStreamWriter(outputStream, "UTF-8");
305 writer.write(postData);
309 outputStream.close();
312 int responseCode = httpConn.getResponseCode();
313 if (responseCode == HttpURLConnection.HTTP_OK)
315 inputStream = httpConn.getInputStream();
317 if (inputStream == null)
319 Log.w(TAG, "httpPost: Response Code Error: " + responseCode + ": " + url);
325 Log.w(TAG, "httpPost: IOException: " + e.getMessage());
327 if (httpConn != null)
329 httpConn.disconnect();
348 if (outputStream != null)
350 outputStream.close();
353 catch (IOException e)
360 BufferedReader reader = null;
361 String replyString = "";
364 StringBuilder responseBuf = new StringBuilder();
365 reader = new BufferedReader(new InputStreamReader(inputStream));
368 while ((c = reader.read()) != -1)
370 responseBuf.append((char) c);
372 replyString = responseBuf.toString();
387 catch (IOException e)
392 return (replyString);