edi233
edi233

Reputation: 3031

Get HTML code from url in android

I was wondering if is any way to get HTML code from any url and save that code as String in code? I have a method:

private String getHtmlData(Context context, String data){
    String head = "<head><style>@font-face {font-family: 'verdana';src: url('file://"+ context.getFilesDir().getAbsolutePath()+ "/verdana.ttf');}body {font-family: 'verdana';}</style></head>";
    String htmlData= "<html>"+head+"<body>"+data+"</body></html>" ;
    return htmlData;
 }

and I want to get this "data" from url. How I can do that?

Upvotes: 1

Views: 7829

Answers (3)

eMi
eMi

Reputation: 5618

Try this (wrote it from the hand)

URL google = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(google.openStream()));
String input;
StringBuffer stringBuffer = new StringBuffer();
while ((input = in.readLine()) != null)
{
    stringBuffer.append(input);
}
in.close();
String htmlData = stringBuffer.toString();

Upvotes: 2

Enrichman
Enrichman

Reputation: 11337

Sure you can. That's actually the response body. You can get it like this:

HttpResponse response = client.execute(post);
String htmlPage = EntityUtils.toString(response.getEntity(), "ISO-8859-1");

Upvotes: 1

Neron T
Neron T

Reputation: 369

take a look at this please, any other parser will work too, or you can even make your own checking the strings and retrieving just the part you want.

Upvotes: 0

Related Questions