Back to project page helsinki-testbed2-android.
The source code is released under:
GNU General Public License
If you think the Android project helsinki-testbed2-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package fi.testbed2.service.impl; /*from w w w. j a v a2s . c o m*/ import com.google.inject.Singleton; import fi.testbed2.R; import fi.testbed2.android.app.Logger; import fi.testbed2.android.task.exception.DownloadTaskException; import fi.testbed2.service.HttpUrlService; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import java.io.IOException; import java.io.InputStream; /** * Service class for making HTTP requests. */ @Singleton public class ApacheHttpUrlService implements HttpUrlService { public ApacheHttpUrlService() { Logger.debug("ApacheHttpUrlService instantiated"); } public InputStream getInputStreamForHttpUrl(final String url) throws DownloadTaskException { try { Logger.debug("getInputStreamForHttpUrl: " + url); DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse response = client.execute(httpGet); StatusLine statusLine = response.getStatusLine(); if(statusLine == null) { throw new DownloadTaskException(R.string.error_msg_http_status, "-1"); } int statusCode = statusLine.getStatusCode(); if(statusCode != HTTP_VALID_STATUS_CODE) { throw new DownloadTaskException(R.string.error_msg_http_status, ""+statusCode); } HttpEntity entity = response.getEntity(); if(entity == null) { throw new DownloadTaskException(R.string.error_msg_http_error); } return entity.getContent(); } catch(IllegalStateException e) { e.printStackTrace(); throw new DownloadTaskException(R.string.error_msg_invalid_url, url); } catch(IllegalArgumentException e) { e.printStackTrace(); throw new DownloadTaskException(R.string.error_msg_invalid_url, url); } catch (ClientProtocolException e) { e.printStackTrace(); throw new DownloadTaskException(R.string.error_msg_http_error); } catch (IOException e) { e.printStackTrace(); throw new DownloadTaskException(R.string.error_msg_http_error); } } }