Back to project page etalio-android-sdk.
The source code is released under:
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions...
If you think the Android project etalio-android-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright 2014 Ericsson AB/* w w w. java2 s. c o m*/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.etalio.android.client; import android.content.Context; import android.os.Build; import com.etalio.android.util.Log; import com.etalio.android.Utils; import com.etalio.android.client.exception.EtalioHttpException; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Default HTTP client for Etalio. Using @link{HttpURLConnection}. */ public class DefaultEtalioHttpClient implements HttpClient { private static final String TAG = DefaultEtalioHttpClient.class.getSimpleName(); public DefaultEtalioHttpClient(final Context context) { disableConnectionReuseIfNecessary(); enableHttpResponseCache(context); } @Override public HttpResponse executeRequest(HttpRequest request, HttpBodyConverter responseConverter, Class responseType) throws EtalioHttpException, IOException { URL url; HttpURLConnection urlConnection=null; try { //URL and connection establishment url = new URL(request.getUrl()); urlConnection = openConnection(url); urlConnection.setDoInput(true); HttpRequest.HttpMethod method = request.getMethod(); urlConnection.setRequestMethod(method.name()); boolean doOutPut = request.getBody() != null && request.getContentLength() > 0; if (doOutPut) { urlConnection.setDoOutput(true); urlConnection.addRequestProperty("Content-Type", request.getMimeType()); urlConnection.setFixedLengthStreamingMode(request.getContentLength()); urlConnection.addRequestProperty("Content-Length", String.valueOf(request.getContentLength())); } if (request.getHeaders() != null) { for (Map.Entry<String, String> header : request.getHeaders().entrySet()) { urlConnection.addRequestProperty(header.getKey(), header.getValue()); } } urlConnection.connect(); if (doOutPut) { OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); writeOutputStream(out, request.getBody()); } Map<String, String> headers = new HashMap<String, String>(); Map<String, List<String>> responseHeaders = urlConnection.getHeaderFields(); if (responseHeaders != null) { for (Map.Entry<String, List<String>> field : responseHeaders.entrySet()) { String name = field.getKey(); for (String value : field.getValue()) { headers.put(name, value); } } } InputStream inputStream; if (urlConnection.getResponseCode() >= 300) { inputStream = urlConnection.getErrorStream(); String error = null; if (inputStream != null) { error = Utils.inputStreamToString(inputStream).toString(); Log.d(TAG, "Error response. Code: " + urlConnection.getResponseCode() + ", Error: " + error); } throw new EtalioHttpException(urlConnection.getResponseCode(), urlConnection.getResponseMessage(), error); } else { inputStream = urlConnection.getInputStream(); } if (true){ StringBuilder str = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { str.append(line); } Log.d(TAG, str.toString()); inputStream = new ByteArrayInputStream(str.toString().getBytes()); } Object responseBody = responseConverter.fromBody(inputStream, "UTF-8", responseType); return new HttpResponse<Object>(urlConnection.getResponseCode(), urlConnection.getResponseMessage(), headers, responseBody); } finally { if(urlConnection != null) urlConnection.disconnect(); } } protected HttpURLConnection openConnection(URL url) throws IOException { return (HttpURLConnection)url.openConnection(); } /** * Helper method writing a byte array to an OutputStream. * @param out The OutputStream for writing to. * @param bytes The byte array to write. */ private void writeOutputStream(OutputStream out, byte[] bytes) { try{ out.write(bytes); out.close(); } catch (Exception e){ Log.e(TAG, "Exception when writing to stream"); } } /** * Disables HTTP connection reuse in Donut and below, as it was buggy From: * http://android-developers.blogspot.com/2011/09/androids-http-clients.html */ private static void disableConnectionReuseIfNecessary() { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.DONUT) { System.setProperty("http.keepAlive", "false"); } } /** * Enables built-in http cache beginning in ICS From: * http://android-developers.blogspot.com/2011/09/androids-http-clients.html * * @param context @see Context to fetch cache directory for. */ private static void enableHttpResponseCache(final Context context) { //noinspection EmptyCatchBlock try { final long httpCacheSize = 10 * 1024 * 1024; // 10 MiB final File httpCacheDir = new File(context.getCacheDir(), "http"); Class.forName("android.net.http.HttpResponseCache").getMethod("install", File.class, long.class).invoke(null, httpCacheDir, httpCacheSize); } catch (final Exception httpResponseCacheNotAvailable) {} } }