Java tutorial
/** * Copyright (C) 2012 Picon software * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package fr.eoidb.util; import java.io.IOException; import java.io.InputStream; import java.util.zip.GZIPInputStream; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.util.Log; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import fr.eo.exception.DownloadException; import fr.eo.xml.util.UrlDownloader; /** * @author picon.software * */ public final class AndroidUrlDownloader implements UrlDownloader { public static final String LOG_TAG = AndroidUrlDownloader.class.getSimpleName(); @Override public InputStream urlToInputStream(Context context, String url) throws DownloadException { if (!isNetworkAvailable(context)) { throw new DownloadException("No internet connection!"); } try { HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established. // The default value is zero, that means the timeout is not used. int timeoutConnection = 3000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. int timeoutSocket = 5000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpClient httpclient = new DefaultHttpClient(httpParameters); HttpGet httpget = new HttpGet(url); httpget.addHeader("Accept-Encoding", "gzip"); HttpResponse response; response = httpclient.execute(httpget); Log.v(LOG_TAG, response.getStatusLine().toString()); HttpEntity entity = response.getEntity(); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { String message = "Error " + statusCode + " while retrieving url " + url; Log.w("AndroidUrlDownloader", message); throw new DownloadException(message); } if (entity != null) { InputStream instream = entity.getContent(); Header contentEncoding = response.getFirstHeader("Content-Encoding"); if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) { Log.v(LOG_TAG, "Accepting gzip for url : " + url); instream = new GZIPInputStream(instream); } return instream; } } catch (IllegalStateException e) { throw new DownloadException(e); } catch (IOException e) { throw new DownloadException(e); } return null; } private boolean isNetworkAvailable(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null; } }