Get a web page programatically from Android
by ricardoz on Nov.18, 2009, under Android, Tips
The Google folks were kind enough to include a version of the Apache HTTP Client in the Android SDK, ergo loading a web resource/page from our Android apps is really simple:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpGet httpGet = new HttpGet("http://www.spartanjava.com"); HttpResponse response = httpClient.execute(httpGet, localContext); String result = ""; BufferedReader reader = new BufferedReader( new InputStreamReader( response.getEntity().getContent() ) ); String line = null; while ((line = reader.readLine()) != null){ result += line + "\n"; } // Now you have the whole HTML loaded on the result variable |
November 10th, 2010 on 6:43 am
Regarding the above code, do we need to import some parent object before compiling it. Can you give reference to the jar file for Appache services.
May 3rd, 2011 on 4:06 am
Maybe that will help:
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.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
June 8th, 2011 on 8:13 am
Hi,
the httpClient.execute method generates the following exception:
UnknownHostException: http://www.spartanjava.com
I set the android.permission.INTERNET to the user, this is surely needed even if not mentioned but it didn’t solve the problem.
I tryed both using my real HW (HTC WildFire) and the AVD (with several configuration).
Any suggestions about how to handle this exception and how to set the android environment ?
Many thk in adv to whoever will spend some time for a reply.
July 17th, 2011 on 9:40 am
@andrea
Try adding the following in your manifest file:
July 17th, 2011 on 11:01 am
@mick
I think you forgot the juicy part :)
July 31st, 2011 on 9:53 am
why doesn’t it work??
well, basically you have to add INTERNET permission in the manifest.
October 5th, 2011 on 8:59 am
thanks for the code.
January 28th, 2012 on 11:45 am
Depending what You want to achieve, much simpler would be using BasicResponseHandler():
…
String response = client.execute(request, new BasicResponseHandler());
have fun!