• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps how to get html page source from a link?

rafiqsab

Lurker
hi all,

i have situation where i need to pass the html page source as a input to the javascript function. so please help me in reading the html page source.


thanks and regards
rafiq
 
This method will return the html of a given URL as a String:


public String getUrlContent(String urlStr) {
try {
URL url = new URL(urlStr);
URLConnection con = null;
con = url.openConnection();
String encoding = con.getContentEncoding();
if (encoding == null) {
encoding = "ISO-8859-1";
}
BufferedReader r = new BufferedReader(new InputStreamReader(con.getInputStream(), encoding));
StringBuilder sb = new StringBuilder();
try {
String s;
while ((s = r.readLine()) != null) {
sb.append(s);
sb.append("\n");
}
} finally {
r.close();
}
return sb.toString();
} catch (IOException ex) {
return "";
}
}
 
Back
Top Bottom