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]
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 byte[] httpGetBytes(String url, int timeoutMs)
132 HttpURLConnection httpConn = null;
133 InputStream inputStream = null;
134 byte[] receivedData = new byte[0];
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 return (receivedData);
165 Log.w(TAG, "httpGet: " + url + " " + e.getMessage());
167 if (httpConn != null)
169 httpConn.disconnect();
171 return (receivedData);
175 BufferedReader reader = null;
179 byte[] buffer = new byte[BUFFER_SIZE];
181 ByteArrayOutputStream out = new ByteArrayOutputStream();
182 reader = new BufferedReader(new InputStreamReader(inputStream));
183 while ((c = reader.read()) != -1)
188 receivedData = out.toByteArray();
189 Log.v(TAG, "RECEIVED " + count + " BYTES. ");
193 Log.w(TAG, "httpGet: exception: " + e.getMessage());
218 return (receivedData);
225 public static Bitmap httpGetBitmap(String url, int timeoutMs)
227 HttpURLConnection httpConn = null;
228 InputStream inputStream = null;
231 int timeout = timeoutMs;
234 timeout = DEFAULT_TIMEOUT;
237 // HTTP GETメソッドで要求を投げる
240 final URL urlObj = new URL(url);
241 httpConn = (HttpURLConnection) urlObj.openConnection();
242 httpConn.setRequestMethod("GET");
243 httpConn.setConnectTimeout(timeout);
244 httpConn.setReadTimeout(timeout);
247 int responseCode = httpConn.getResponseCode();
248 if (responseCode == HttpURLConnection.HTTP_OK)
250 inputStream = httpConn.getInputStream();
251 if (inputStream != null)
253 bmp = BitmapFactory.decodeStream(inputStream);
256 if (inputStream == null)
258 Log.w(TAG, "httpGet: Response Code Error: " + responseCode + ": " + url);
265 Log.w(TAG, "httpGet: " + url + " " + e.getMessage());
267 if (httpConn != null)
269 httpConn.disconnect();
281 public static String httpPost(String url, String postData, int timeoutMs)
283 HttpURLConnection httpConn = null;
284 OutputStream outputStream = null;
285 OutputStreamWriter writer = null;
286 InputStream inputStream = null;
288 int timeout = timeoutMs;
291 timeout = DEFAULT_TIMEOUT;
294 // HTTP Postメソッドで要求を送出
297 final URL urlObj = new URL(url);
298 httpConn = (HttpURLConnection) urlObj.openConnection();
299 httpConn.setRequestMethod("POST");
300 httpConn.setConnectTimeout(timeout);
301 httpConn.setReadTimeout(timeout);
302 httpConn.setDoInput(true);
303 httpConn.setDoOutput(true);
305 outputStream = httpConn.getOutputStream();
306 writer = new OutputStreamWriter(outputStream, "UTF-8");
307 writer.write(postData);
311 outputStream.close();
314 int responseCode = httpConn.getResponseCode();
315 if (responseCode == HttpURLConnection.HTTP_OK)
317 inputStream = httpConn.getInputStream();
319 if (inputStream == null)
321 Log.w(TAG, "httpPost: Response Code Error: " + responseCode + ": " + url);
327 Log.w(TAG, "httpPost: IOException: " + e.getMessage());
329 if (httpConn != null)
331 httpConn.disconnect();
350 if (outputStream != null)
352 outputStream.close();
355 catch (IOException e)
362 BufferedReader reader = null;
363 String replyString = "";
366 StringBuilder responseBuf = new StringBuilder();
367 reader = new BufferedReader(new InputStreamReader(inputStream));
370 while ((c = reader.read()) != -1)
372 responseBuf.append((char) c);
374 replyString = responseBuf.toString();
389 catch (IOException e)
394 return (replyString);