Executing a HTTP GET Request with HttpClient

1 vote · 4 comments

Retrieve the contents of a stream via a HTTP GET.

raw ·
copy
· download
public static InputStream getInputStreamFromUrl(String url) { InputStream content = null; try { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(new HttpGet(url)); content = response.getEntity().getContent(); } catch (Exception e) { Log.("[GET REQUEST]", "Network exception", e); } return content; }
Add a comment

4 Comments

Nice, but it should be:

Log.e("[GET REQUEST]", "Network exception");

instead of

Log.("[GET REQUEST]", "Network exception", e);

;)

Tom

Reply · Sept. 8, 2009, 8:03 p.m.

@Tom: Why? If you pass the Throwable (Exception) it'll log it.

Reply · Sept. 9, 2009, 1 p.m.

should this be in a thread or the HttpClient have it's own thread?

Reply · Sept. 12, 2009, 1:45 a.m.

Sidenote: You can read the InputStream into a String with this sippet: http://www.androidsnippets.com/get-the-content-from-a-httpresponse-or-any-inputstream-as-a-string

Reply · March 8, 2011, 11:07 p.m.