0

I am trying to get the HTML source from a URL in my Android app using the code below. However, it crashes at the line HttpResponse response = client.execute(request);. How can I fix this?

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);

String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
    str.append(line);
}
in.close();
html = str.toString();

UPDATE! I am too new of a user to post my own official answer apparently, so here it is:

Thanks to both of you! Hichris' comments & links pointed me in the right direction. Here are the 3 hurdles that were causing the problem:

  1. The AsyncTask should be placed in the same class file, but as a sub-class (I might not have my terminology right there)
  2. I was filling a text box with the resulting html code. However, I was doing that within the doInBackground() section of the AsyncTask when I should have been doing it in onPostExecute().
  3. The urls I was passing to the AsyncTask were not properly converted to URI. This caused the program to crash for some urls but not others.
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
  • 2
    This is probably because of a `NetworkOnMainThreadException`. Move this to an AsyncTask, and it'll fix it. – hichris123 Mar 29 '14 at 16:27
  • I tried creating an AsyncTask and I got the same error. However, I am very new to java so I probably did it wrong. How exactly do I create an AsyncTask? For example, does it go in a new java class file, or can I add it to the MainActivity file? I found the google instructions for AsyncTasks, but they do not explain where to put the actual code... – user3476152 Mar 29 '14 at 16:34
  • Check [this answer](http://stackoverflow.com/a/6343299/2581872), hopefully it'll give you an idea on where to put what code. And it should be in the same class file, however, it should be declared as a separate, inner class. – hichris123 Mar 29 '14 at 16:38
  • Thanks hichris, using the link you provided, I added the AsyncTask. Now if I press the button once (I use onclick to activate this code) it fails. However, if I press the button repeatedly it works. Any thoughts on that one? I am loading www.google.com. – user3476152 Mar 29 '14 at 17:11
  • Also, it appears to consistently crash for some sites but not others. This is ridiculous. – user3476152 Mar 29 '14 at 17:47
  • Ok, I figured out the crashing. I was filling a text box with the resulting html code. However, I was doing that within the doInBackground() section of the AsyncTask when I should have been doing it in onPostExecute(). It apparently was overloading the system, or something like that. I still can't figure out why it works when pressing the button repeatedly several times but fails if I just press it once slowly. – user3476152 Mar 29 '14 at 18:01

1 Answers1

0

The best way to use for network call is Volley library check this link "https://developers.google.com/events/io/sessions/325304728".It is very simple and very easy to use. Check the below link for what are the problem in creating HTTPClient and other third party library for network call "https://www.youtube.com/watch?v=MIc4kl3yXw0&list=LLckOGLeNzdRsRG5CvBux_dg&index=8"

vicky
  • 340
  • 5
  • 13