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.eoit.util; import android.content.Context; import fr.eo.exception.DownloadException; import fr.eo.xml.util.UrlDownloader; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.InputStream; import java.util.zip.GZIPInputStream; /** * @author picon.software * */ public final class DownloadUtil implements UrlDownloader { private static final Logger LOGGER = LoggerFactory.getLogger(DownloadUtil.class); @Override public InputStream urlToInputStream(Context context, String url) throws DownloadException { try { HttpClient httpclient = new DefaultHttpClient(); //HttpHost proxy = new HttpHost("httppxlyon1.srv.volvo.com", 8080); //httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); HttpGet httpget = new HttpGet(url); httpget.addHeader("Accept-Encoding", "gzip"); HttpResponse response; response = httpclient.execute(httpget); LOGGER.debug(response.getStatusLine().toString()); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); Header contentEncoding = response.getFirstHeader("Content-Encoding"); if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) { LOGGER.debug("Accepting gzip for url : " + url); instream = new GZIPInputStream(instream); } return instream; } } catch (ClientProtocolException e) { throw new DownloadException(e); } catch (IOException e) { throw new DownloadException(e); } return null; } }