2 * Copyright 2014 Gustavo Martin Morcuende
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package name.gumartinm.weather.information.httpclient;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.UnsupportedEncodingException;
22 import java.net.URISyntaxException;
25 import org.apache.http.HttpEntity;
26 import org.apache.http.HttpResponse;
27 import org.apache.http.HttpStatus;
28 import org.apache.http.client.ClientProtocolException;
29 import org.apache.http.client.ResponseHandler;
30 import org.apache.http.client.methods.HttpGet;
32 import android.net.http.AndroidHttpClient;
34 public class CustomHTTPClient {
35 private final AndroidHttpClient httpClient;
37 public CustomHTTPClient(final AndroidHttpClient httpClient) {
38 this.httpClient = httpClient;
41 public String retrieveDataAsString(final URL url)
42 throws URISyntaxException, ClientProtocolException, IOException {
44 final ResponseHandler<String> handler = new ResponseHandler<String>() {
46 public String handleResponse(
47 final HttpResponse response)
48 throws UnsupportedEncodingException, IOException {
50 if (response != null) {
51 final HttpEntity entity = response.getEntity();
54 final ContentType contentType = ContentType.getOrDefault(entity);
55 final ByteArrayOutputStream buffer = CustomHTTPClient.this
56 .sortResponse(response);
57 return new String(buffer.toByteArray(), contentType.getCharset());
59 entity.consumeContent();
63 throw new IOException("There is no entity");
66 throw new IOException("There is no response");
70 final HttpGet httpGet = new HttpGet();
71 httpGet.setURI(url.toURI());
73 return this.httpClient.execute(httpGet, handler);
76 public ByteArrayOutputStream retrieveRawData(final URL url)
77 throws URISyntaxException, ClientProtocolException, IOException {
78 final ResponseHandler<ByteArrayOutputStream> handler = new ResponseHandler<ByteArrayOutputStream>() {
81 public ByteArrayOutputStream handleResponse(
82 final HttpResponse response)
83 throws UnsupportedEncodingException, IOException {
85 if (response != null) {
86 final HttpEntity entity = response.getEntity();
89 return CustomHTTPClient.this.sortResponse(response);
91 entity.consumeContent();
95 throw new IOException("There is no entity");
98 throw new IOException("There is no response");
102 final HttpGet httpGet = new HttpGet();
103 httpGet.setURI(url.toURI());
105 return this.httpClient.execute(httpGet, handler);
108 public void close() {
109 this.httpClient.close();
112 private ByteArrayOutputStream sortResponse(final HttpResponse httpResponse) throws IOException {
114 if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
115 throw new IOException("Unexpected response code: "
116 + httpResponse.getStatusLine().getStatusCode());
119 final HttpEntity entity = httpResponse.getEntity();
120 final InputStream inputStream = entity.getContent();
122 return this.readInputStream(inputStream);
129 private ByteArrayOutputStream readInputStream (final InputStream inputStream) throws IOException {
130 final ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
131 final int bufferSize = 1024;
132 final byte[] buffer = new byte[bufferSize];
135 while ((len = inputStream.read(buffer)) != -1) {
136 byteBuffer.write(buffer, 0, len);