1 package name.gumartinm.weather.information.httpclient;
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.UnsupportedEncodingException;
7 import java.net.URISyntaxException;
10 import org.apache.http.HttpEntity;
11 import org.apache.http.HttpResponse;
12 import org.apache.http.HttpStatus;
13 import org.apache.http.client.ClientProtocolException;
14 import org.apache.http.client.ResponseHandler;
15 import org.apache.http.client.methods.HttpGet;
17 import android.net.http.AndroidHttpClient;
19 public class CustomHTTPClient {
20 private final AndroidHttpClient httpClient;
22 public CustomHTTPClient(final AndroidHttpClient httpClient) {
23 this.httpClient = httpClient;
26 public String retrieveDataAsString(final URL url)
27 throws URISyntaxException, ClientProtocolException, IOException {
29 final ResponseHandler<String> handler = new ResponseHandler<String>() {
31 public String handleResponse(
32 final HttpResponse response)
33 throws UnsupportedEncodingException, IOException {
35 if (response != null) {
36 final HttpEntity entity = response.getEntity();
39 final ContentType contentType = ContentType.getOrDefault(entity);
40 final ByteArrayOutputStream buffer = CustomHTTPClient.this
41 .sortResponse(response);
42 return new String(buffer.toByteArray(), contentType.getCharset());
44 entity.consumeContent();
48 throw new IOException("There is no entity");
51 throw new IOException("There is no response");
55 final HttpGet httpGet = new HttpGet();
56 httpGet.setURI(url.toURI());
58 return this.httpClient.execute(httpGet, handler);
61 public ByteArrayOutputStream retrieveRawData(final URL url)
62 throws URISyntaxException, ClientProtocolException, IOException {
63 final ResponseHandler<ByteArrayOutputStream> handler = new ResponseHandler<ByteArrayOutputStream>() {
66 public ByteArrayOutputStream handleResponse(
67 final HttpResponse response)
68 throws UnsupportedEncodingException, IOException {
70 if (response != null) {
71 final HttpEntity entity = response.getEntity();
74 return CustomHTTPClient.this.sortResponse(response);
76 entity.consumeContent();
80 throw new IOException("There is no entity");
83 throw new IOException("There is no response");
87 final HttpGet httpGet = new HttpGet();
88 httpGet.setURI(url.toURI());
90 return this.httpClient.execute(httpGet, handler);
94 this.httpClient.close();
97 private ByteArrayOutputStream sortResponse(final HttpResponse httpResponse) throws IOException {
99 if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
100 throw new IOException("Unexpected response code: "
101 + httpResponse.getStatusLine().getStatusCode());
104 final HttpEntity entity = httpResponse.getEntity();
105 final InputStream inputStream = entity.getContent();
107 return this.readInputStream(inputStream);
114 private ByteArrayOutputStream readInputStream (final InputStream inputStream) throws IOException {
115 final ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
116 final int bufferSize = 1024;
117 final byte[] buffer = new byte[bufferSize];
120 while ((len = inputStream.read(buffer)) != -1) {
121 byteBuffer.write(buffer, 0, len);