Friday, 27 September 2013

Download HTML source of webpage not getting the right data

Download HTML source of webpage not getting the right data

I'm trying to download the HTML source of a website
(http://thefuckingweather.com) to a String value for later manipulation.
The code I have listed below works for every other website I've tried, but
for some reason when I run it with the website listed above, I get the
following result:
The page you requested was removed.
The problem is that I can access that exact URL via the browser and that
is clearly not what's being displayed.. Any ideas? Here's my code:
public void getHtml(final String website) throws ClientProtocolException,
IOException
{
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
HttpClient httpclient = new DefaultHttpClient(); // Create
HTTP Client
HttpGet httpget = new HttpGet(website); // Set the action
you want to do
HttpResponse response = httpclient.execute(httpget); //
Executeit
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent(); // Create an
InputStream with the response
BufferedReader reader = new BufferedReader(new
InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) // Read line by
line
sb.append(line + "\n");
String resString = sb.toString(); // Result is here
is.close(); // Close the stream
SettingsActivity.stringReturn=resString;
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
And in OnCreate() I call:
try {
getHtml("http://thefuckingweather.com");
while(stringReturn.equals("")){
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

No comments:

Post a Comment