From: Gustavo Martin Morcuende Date: Mon, 29 Aug 2016 19:23:26 +0000 (+0200) Subject: AndroidHttpClient is deprecated X-Git-Tag: weatherinformation-1.2~8 X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=1e132e464134d37cf50cb16e54f6cbcb2ccc1502;p=AndroidWeatherInformation AndroidHttpClient is deprecated --- diff --git a/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java b/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java index 7cdbeba..1eaca7e 100644 --- a/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java +++ b/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java @@ -25,8 +25,6 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; -import org.apache.http.client.ClientProtocolException; - import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -34,7 +32,6 @@ import android.content.IntentFilter; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.net.http.AndroidHttpClient; import android.os.AsyncTask; import android.os.Bundle; import android.preference.PreferenceManager; @@ -186,7 +183,7 @@ public class CurrentFragment extends Fragment { this.getActivity().findViewById(R.id.weather_current_error_message).setVisibility(View.GONE); final CurrentTask task = new CurrentTask( this.getActivity().getApplicationContext(), - new CustomHTTPClient(AndroidHttpClient.newInstance(this.getString(R.string.http_client_agent))), + CustomHTTPClient.newInstance(this.getString(R.string.http_client_agent)), new ServiceCurrentParser(new JPOSCurrentParser())); task.execute(weatherLocation.getLatitude(), weatherLocation.getLongitude()); @@ -402,8 +399,6 @@ public class CurrentFragment extends Fragment { current = this.doInBackgroundThrowable(latitude, longitude); } catch (final JsonParseException e) { Timber.e(e, "CurrentTask doInBackground exception: "); - } catch (final ClientProtocolException e) { - Timber.e(e, "CurrentTask doInBackground exception: "); } catch (final MalformedURLException e) { Timber.e(e, "CurrentTask doInBackground exception: "); } catch (final URISyntaxException e) { @@ -415,15 +410,13 @@ public class CurrentFragment extends Fragment { // I loathe doing this but we must show some error to our dear user by means // of returning Forecast null value. Timber.e(e, "CurrentTask doInBackground exception: "); - } finally { - HTTPClient.close(); } return current; } private Current doInBackgroundThrowable(final double latitude, final double longitude) - throws URISyntaxException, ClientProtocolException, JsonParseException, IOException { + throws URISyntaxException, JsonParseException, IOException { final SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(localContext.getApplicationContext()); final String APPID = sharedPreferences.getString(localContext.getString(R.string.weather_preferences_app_id_key), ""); diff --git a/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java b/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java index 852a0be..1807244 100644 --- a/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java +++ b/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java @@ -15,21 +15,6 @@ */ package name.gumartinm.weather.information.fragment.overview; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; -import java.text.DecimalFormat; -import java.text.NumberFormat; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.Locale; - -import org.apache.http.client.ClientProtocolException; - import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -37,7 +22,6 @@ import android.content.IntentFilter; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.net.http.AndroidHttpClient; import android.os.AsyncTask; import android.os.Bundle; import android.preference.PreferenceManager; @@ -47,6 +31,20 @@ import android.view.View; import android.widget.ListView; import com.fasterxml.jackson.core.JsonParseException; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.Locale; + import name.gumartinm.weather.information.R; import name.gumartinm.weather.information.activity.SpecificActivity; import name.gumartinm.weather.information.fragment.specific.SpecificFragment; @@ -163,7 +161,7 @@ public class OverviewFragment extends ListFragment { this.setListShownNoAnimation(false); final OverviewTask task = new OverviewTask( this.getActivity().getApplicationContext(), - new CustomHTTPClient(AndroidHttpClient.newInstance(this.getString(R.string.http_client_agent))), + CustomHTTPClient.newInstance(this.getString(R.string.http_client_agent)), new ServiceForecastParser(new JPOSForecastParser())); task.execute(weatherLocation.getLatitude(), weatherLocation.getLongitude()); @@ -329,8 +327,6 @@ public class OverviewFragment extends ListFragment { forecast = this.doInBackgroundThrowable(latitude, longitude); } catch (final JsonParseException e) { Timber.e(e, "OverviewTask doInBackground exception: "); - } catch (final ClientProtocolException e) { - Timber.e(e, "OverviewTask doInBackground exception: "); } catch (final MalformedURLException e) { Timber.e(e, "OverviewTask doInBackground exception: "); } catch (final URISyntaxException e) { @@ -342,15 +338,13 @@ public class OverviewFragment extends ListFragment { // I loathe doing this but we must show some error to our dear user by means // of returning Forecast null value. Timber.e(e, "OverviewTask doInBackground exception: "); - } finally { - HTTPClient.close(); } return forecast; } private Forecast doInBackgroundThrowable(final double latitude, final double longitude) - throws URISyntaxException, ClientProtocolException, JsonParseException, IOException { + throws URISyntaxException, JsonParseException, IOException { final SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(localContext.getApplicationContext()); final String APPID = sharedPreferences.getString(localContext.getString(R.string.weather_preferences_app_id_key), ""); diff --git a/app/src/main/java/name/gumartinm/weather/information/httpclient/Consts.java b/app/src/main/java/name/gumartinm/weather/information/httpclient/Consts.java deleted file mode 100644 index 5cea42a..0000000 --- a/app/src/main/java/name/gumartinm/weather/information/httpclient/Consts.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ - -package name.gumartinm.weather.information.httpclient; - -import java.nio.charset.Charset; - -/** - * Commons constants. - * - * @since 4.2 - */ -public final class Consts { - - public static final int CR = 13; // - public static final int LF = 10; // - public static final int SP = 32; // - public static final int HT = 9; // - - public static final Charset UTF_8 = Charset.forName("UTF-8"); - public static final Charset ASCII = Charset.forName("US-ASCII"); - public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); - - private Consts() { - } - -} diff --git a/app/src/main/java/name/gumartinm/weather/information/httpclient/ContentType.java b/app/src/main/java/name/gumartinm/weather/information/httpclient/ContentType.java deleted file mode 100644 index 27a8b3d..0000000 --- a/app/src/main/java/name/gumartinm/weather/information/httpclient/ContentType.java +++ /dev/null @@ -1,248 +0,0 @@ -/* - * ==================================================================== - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - * - */ - -package name.gumartinm.weather.information.httpclient; - -import java.nio.charset.Charset; -import java.nio.charset.UnsupportedCharsetException; -import java.util.Locale; - -import org.apache.http.Header; -import org.apache.http.HeaderElement; -import org.apache.http.HttpEntity; -import org.apache.http.NameValuePair; -import org.apache.http.ParseException; -import org.apache.http.message.BasicHeaderValueParser; - -/** - * Content type information consisting of a MIME type and an optional charset. - *

- * This class makes no attempts to verify validity of the MIME type. - * The input parameters of the {@link #create(String, String)} method, however, may not - * contain characters <">, <;>, <,> reserved by the HTTP specification. - * - * @since 4.2 - */ - -public final class ContentType { - - // constants - public static final ContentType APPLICATION_ATOM_XML = create( - "application/atom+xml", Consts.ISO_8859_1); - public static final ContentType APPLICATION_FORM_URLENCODED = create( - "application/x-www-form-urlencoded", Consts.ISO_8859_1); - public static final ContentType APPLICATION_JSON = create( - "application/json", Consts.UTF_8); - public static final ContentType APPLICATION_OCTET_STREAM = create( - "application/octet-stream", (Charset) null); - public static final ContentType APPLICATION_SVG_XML = create( - "application/svg+xml", Consts.ISO_8859_1); - public static final ContentType APPLICATION_XHTML_XML = create( - "application/xhtml+xml", Consts.ISO_8859_1); - public static final ContentType APPLICATION_XML = create( - "application/xml", Consts.ISO_8859_1); - public static final ContentType MULTIPART_FORM_DATA = create( - "multipart/form-data", Consts.ISO_8859_1); - public static final ContentType TEXT_HTML = create( - "text/html", Consts.ISO_8859_1); - public static final ContentType TEXT_PLAIN = create( - "text/plain", Consts.ISO_8859_1); - public static final ContentType TEXT_XML = create( - "text/xml", Consts.ISO_8859_1); - public static final ContentType WILDCARD = create( - "*/*", (Charset) null); - - // defaults - public static final ContentType DEFAULT_TEXT = TEXT_PLAIN; - public static final ContentType DEFAULT_BINARY = APPLICATION_OCTET_STREAM; - - private final String mimeType; - private final Charset charset; - - /** - * Given a MIME type and a character set, constructs a ContentType. - * @param mimeType The MIME type to use for the ContentType header. - * @param charset The optional character set to use with the ContentType header. - * @throws UnsupportedCharsetException - * If no support for the named charset is available in this Java virtual machine - */ - ContentType(final String mimeType, final Charset charset) { - this.mimeType = mimeType; - this.charset = charset; - } - - public String getMimeType() { - return this.mimeType; - } - - public Charset getCharset() { - return this.charset; - } - - /** - * Converts a ContentType to a string which can be used as a ContentType header. - * If a charset is provided by the ContentType, it will be included in the string. - */ - @Override - public String toString() { - final StringBuilder buf = new StringBuilder(); - buf.append(this.mimeType); - if (this.charset != null) { - buf.append("; charset="); - buf.append(this.charset); - } - return buf.toString(); - } - - private static boolean valid(final String s) { - for (int i = 0; i < s.length(); i++) { - final char ch = s.charAt(i); - if ((ch == '"') || (ch == ',') || (ch == ';')) { - return false; - } - } - return true; - } - - /** - * Creates a new instance of {@link ContentType}. - * - * @param mimeType MIME type. It may not be null or empty. It may not contain - * characters <">, <;>, <,> reserved by the HTTP specification. - * @param charset charset. - * @return content type - */ - public static ContentType create(final String mimeType, final Charset charset) { - if (mimeType == null) { - throw new IllegalArgumentException("MIME type may not be null"); - } - final String type = mimeType.trim().toLowerCase(Locale.US); - if (type.length() == 0) { - throw new IllegalArgumentException("MIME type may not be empty"); - } - if (!valid(type)) { - throw new IllegalArgumentException("MIME type may not contain reserved characters"); - } - return new ContentType(type, charset); - } - - /** - * Creates a new instance of {@link ContentType} without a charset. - * - * @param mimeType MIME type. It may not be null or empty. It may not contain - * characters <">, <;>, <,> reserved by the HTTP specification. - * @return content type - */ - public static ContentType create(final String mimeType) { - return new ContentType(mimeType, (Charset) null); - } - - /** - * Creates a new instance of {@link ContentType}. - * - * @param mimeType MIME type. It may not be null or empty. It may not contain - * characters <">, <;>, <,> reserved by the HTTP specification. - * @param charset charset. It may not contain characters <">, <;>, <,> reserved by the HTTP - * specification. This parameter is optional. - * @return content type - */ - public static ContentType create( - final String mimeType, final String charset) throws UnsupportedCharsetException { - return create(mimeType, charset != null ? Charset.forName(charset) : null); - } - - private static ContentType create(final HeaderElement helem) { - final String mimeType = helem.getName(); - String charset = null; - final NameValuePair param = helem.getParameterByName("charset"); - if (param != null) { - charset = param.getValue(); - } - return create(mimeType, charset); - } - - /** - * Parses textual representation of Content-Type value. - * - * @param s text - * @return content type - * @throws ParseException if the given text does not represent a valid - * Content-Type value. - */ - public static ContentType parse( - final String s) throws ParseException, UnsupportedCharsetException { - if (s == null) { - throw new IllegalArgumentException("Content type may not be null"); - } - final HeaderElement[] elements = BasicHeaderValueParser.parseElements(s, null); - if (elements.length > 0) { - return create(elements[0]); - } else { - throw new ParseException("Invalid content type: " + s); - } - } - - /** - * Extracts Content-Type value from {@link HttpEntity} exactly as - * specified by the Content-Type header of the entity. Returns null - * if not specified. - * - * @param entity HTTP entity - * @return content type - * @throws ParseException if the given text does not represent a valid - * Content-Type value. - */ - public static ContentType get( - final HttpEntity entity) throws ParseException, UnsupportedCharsetException { - if (entity == null) { - return null; - } - final Header header = entity.getContentType(); - if (header != null) { - final HeaderElement[] elements = header.getElements(); - if (elements.length > 0) { - return create(elements[0]); - } - } - return null; - } - - /** - * Extracts Content-Type value from {@link HttpEntity} or returns default value - * if not explicitly specified. - * - * @param entity HTTP entity - * @return content type - * @throws ParseException if the given text does not represent a valid - * Content-Type value. - */ - public static ContentType getOrDefault(final HttpEntity entity) throws ParseException { - final ContentType contentType = get(entity); - return contentType != null ? contentType : DEFAULT_TEXT; - } - -} diff --git a/app/src/main/java/name/gumartinm/weather/information/httpclient/CustomHTTPClient.java b/app/src/main/java/name/gumartinm/weather/information/httpclient/CustomHTTPClient.java index f981d24..275706a 100644 --- a/app/src/main/java/name/gumartinm/weather/information/httpclient/CustomHTTPClient.java +++ b/app/src/main/java/name/gumartinm/weather/information/httpclient/CustomHTTPClient.java @@ -15,115 +15,30 @@ */ package name.gumartinm.weather.information.httpclient; +import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.UnsupportedEncodingException; -import java.net.URISyntaxException; +import java.net.HttpURLConnection; import java.net.URL; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpGet; - -import android.net.http.AndroidHttpClient; - public class CustomHTTPClient { - private final AndroidHttpClient httpClient; - - public CustomHTTPClient(final AndroidHttpClient httpClient) { - this.httpClient = httpClient; - } - - public String retrieveDataAsString(final URL url) - throws URISyntaxException, ClientProtocolException, IOException { - - final ResponseHandler handler = new ResponseHandler() { - @Override - public String handleResponse( - final HttpResponse response) - throws UnsupportedEncodingException, IOException { - - if (response != null) { - final HttpEntity entity = response.getEntity(); - if (entity != null) { - try { - final ContentType contentType = ContentType.getOrDefault(entity); - final ByteArrayOutputStream buffer = CustomHTTPClient.this - .sortResponse(response); - return new String(buffer.toByteArray(), contentType.getCharset()); - } finally { - entity.consumeContent(); - } - } - - throw new IOException("There is no entity"); - } - - throw new IOException("There is no response"); - } - }; - - final HttpGet httpGet = new HttpGet(); - httpGet.setURI(url.toURI()); - - return this.httpClient.execute(httpGet, handler); - } - - public ByteArrayOutputStream retrieveRawData(final URL url) - throws URISyntaxException, ClientProtocolException, IOException { - final ResponseHandler handler = new ResponseHandler() { + private final String userAgent; - @Override - public ByteArrayOutputStream handleResponse( - final HttpResponse response) - throws UnsupportedEncodingException, IOException { - - if (response != null) { - final HttpEntity entity = response.getEntity(); - if (entity != null) { - try { - return CustomHTTPClient.this.sortResponse(response); - } finally { - entity.consumeContent(); - } - } - - throw new IOException("There is no entity"); - } - - throw new IOException("There is no response"); - } - }; - - final HttpGet httpGet = new HttpGet(); - httpGet.setURI(url.toURI()); - - return this.httpClient.execute(httpGet, handler); - } - - public void close() { - this.httpClient.close(); + private CustomHTTPClient(String userAgent) { + this.userAgent = userAgent; } - private ByteArrayOutputStream sortResponse(final HttpResponse httpResponse) throws IOException { - - if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { - throw new IOException("Unexpected response code: " - + httpResponse.getStatusLine().getStatusCode()); - } - - final HttpEntity entity = httpResponse.getEntity(); - final InputStream inputStream = entity.getContent(); + public String retrieveDataAsString(final URL url) throws IOException { + final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); + urlConnection.setRequestProperty("User-Agent", userAgent); try { - return this.readInputStream(inputStream); + final InputStream in = new BufferedInputStream(urlConnection.getInputStream()); + final ByteArrayOutputStream buffer = readInputStream(in); + return new String(buffer.toByteArray() /**, contentType.getCharset()**/); } finally { - inputStream.close(); + urlConnection.disconnect(); } - } private ByteArrayOutputStream readInputStream (final InputStream inputStream) throws IOException { @@ -138,4 +53,8 @@ public class CustomHTTPClient { return byteBuffer; } + + public static final CustomHTTPClient newInstance(String userAgent) { + return new CustomHTTPClient(userAgent); + } } diff --git a/app/src/main/java/name/gumartinm/weather/information/notification/NotificationIntentService.java b/app/src/main/java/name/gumartinm/weather/information/notification/NotificationIntentService.java index eb5a2b2..40e128b 100644 --- a/app/src/main/java/name/gumartinm/weather/information/notification/NotificationIntentService.java +++ b/app/src/main/java/name/gumartinm/weather/information/notification/NotificationIntentService.java @@ -15,16 +15,6 @@ */ package name.gumartinm.weather.information.notification; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; -import java.text.DecimalFormat; -import java.text.NumberFormat; -import java.util.Locale; - -import org.apache.http.client.ClientProtocolException; - import android.app.IntentService; import android.app.Notification; import android.app.PendingIntent; @@ -32,7 +22,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.net.http.AndroidHttpClient; import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; @@ -40,6 +29,15 @@ import android.support.v4.app.TaskStackBuilder; import android.widget.RemoteViews; import com.fasterxml.jackson.core.JsonParseException; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.Locale; + import name.gumartinm.weather.information.R; import name.gumartinm.weather.information.activity.MainTabsActivity; import name.gumartinm.weather.information.httpclient.CustomHTTPClient; @@ -67,8 +65,7 @@ public class NotificationIntentService extends IntentService { if (weatherLocation != null) { final ServiceCurrentParser weatherService = new ServiceCurrentParser(new JPOSCurrentParser()); - final CustomHTTPClient HTTPClient = new CustomHTTPClient( - AndroidHttpClient.newInstance(this.getString(R.string.http_client_agent))); + final CustomHTTPClient HTTPClient = CustomHTTPClient.newInstance(this.getString(R.string.http_client_agent)); Current current = null; try { @@ -76,8 +73,6 @@ public class NotificationIntentService extends IntentService { } catch (final JsonParseException e) { Timber.e(e, "doInBackground exception: "); - } catch (final ClientProtocolException e) { - Timber.e(e, "doInBackground exception: "); } catch (final MalformedURLException e) { Timber.e(e, "doInBackground exception: "); } catch (final URISyntaxException e) { @@ -85,8 +80,6 @@ public class NotificationIntentService extends IntentService { } catch (final IOException e) { // logger infrastructure swallows UnknownHostException :/ Timber.e(e, "doInBackground exception: " + e.getMessage()); - } finally { - HTTPClient.close(); } if (current != null) { @@ -97,7 +90,7 @@ public class NotificationIntentService extends IntentService { private Current doInBackgroundThrowable(final WeatherLocation weatherLocation, final CustomHTTPClient HTTPClient, final ServiceCurrentParser weatherService) - throws ClientProtocolException, MalformedURLException, URISyntaxException, + throws MalformedURLException, URISyntaxException, JsonParseException, IOException { final SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this.getApplicationContext()); diff --git a/app/src/main/java/name/gumartinm/weather/information/widget/WidgetIntentService.java b/app/src/main/java/name/gumartinm/weather/information/widget/WidgetIntentService.java index de33dde..08d0903 100644 --- a/app/src/main/java/name/gumartinm/weather/information/widget/WidgetIntentService.java +++ b/app/src/main/java/name/gumartinm/weather/information/widget/WidgetIntentService.java @@ -24,13 +24,21 @@ import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; -import android.net.http.AndroidHttpClient; import android.preference.PreferenceManager; import android.support.v4.app.TaskStackBuilder; import android.view.View; import android.widget.RemoteViews; import com.fasterxml.jackson.core.JsonParseException; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.Locale; + import name.gumartinm.weather.information.R; import name.gumartinm.weather.information.httpclient.CustomHTTPClient; import name.gumartinm.weather.information.model.DatabaseQueries; @@ -42,16 +50,6 @@ import name.gumartinm.weather.information.service.PermanentStorage; import name.gumartinm.weather.information.service.ServiceCurrentParser; import timber.log.Timber; -import org.apache.http.client.ClientProtocolException; - -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; -import java.text.DecimalFormat; -import java.text.NumberFormat; -import java.util.Locale; - public class WidgetIntentService extends IntentService { private static final String WIDGET_PREFERENCES_NAME = "WIDGET_PREFERENCES"; @@ -145,16 +143,13 @@ public class WidgetIntentService extends IntentService { private Current getRemoteCurrent(final WeatherLocation weatherLocation) { final ServiceCurrentParser weatherService = new ServiceCurrentParser(new JPOSCurrentParser()); - final CustomHTTPClient HTTPClient = new CustomHTTPClient( - AndroidHttpClient.newInstance(this.getString(R.string.http_client_agent))); + final CustomHTTPClient HTTPClient = CustomHTTPClient.newInstance(this.getString(R.string.http_client_agent)); try { return this.getRemoteCurrentThrowable(weatherLocation, HTTPClient, weatherService); } catch (final JsonParseException e) { Timber.e(e, "doInBackground exception: "); - } catch (final ClientProtocolException e) { - Timber.e(e, "doInBackground exception: "); } catch (final MalformedURLException e) { Timber.e(e, "doInBackground exception: "); } catch (final URISyntaxException e) { @@ -162,8 +157,6 @@ public class WidgetIntentService extends IntentService { } catch (final IOException e) { // logger infrastructure swallows UnknownHostException :/ Timber.e(e, "doInBackground exception: " + e.getMessage()); - } finally { - HTTPClient.close(); } return null; @@ -171,7 +164,7 @@ public class WidgetIntentService extends IntentService { private Current getRemoteCurrentThrowable(final WeatherLocation weatherLocation, final CustomHTTPClient HTTPClient, final ServiceCurrentParser weatherService) - throws ClientProtocolException, MalformedURLException, URISyntaxException, + throws MalformedURLException, URISyntaxException, JsonParseException, IOException { final SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this.getApplicationContext());