I’m trying to download a website source code and display it in a textbox but I seem to get an error and can’t figure it out :s
public void getHtml() throws ClientProtocolException, IOException
{
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";
Toast.makeText(activity.this, line.toString(), Toast.LENGTH_LONG).show();
}
}
how come this doesn’t work and throw an IOException?
I think you’re probably missing INTERNET permission in your manifest.xml
Pay attention to
<uses-permission>tag provided in the code below. I tested your code in eclipse, and it works.BTW I think using
String resultin this way wont work. Didn’t test that far though. But I think you cannot just add string to a string. You need to usestringBuilderand append new strings.EDIT: tested this
String resultmetod, and it works. Maybe the problem is that you are trying to throw so many toast all at once. Your code throws a toast for every line of retrived html code. I set yourgetHtml()method to type String, and to returnresult, and it returned it properly… I can’t think of any other reason for exception, except missing INTERNET permission in your AndroidManifest.xml….Cheers!