1. Download our Official Android App: Forums for Android!

Apps how to get html page source from a link?

Discussion in 'Android Development' started by rafiqsab, Jul 13, 2011.

  1. rafiqsab

    rafiqsab Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    8
    Joined:
    Jul 13, 2011

    Jul 13, 2011
    8
    0
    5
    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
     

    Advertisement

  2. dCoder

    dCoder Newbie
    Rank:
    None
    Points:
    16
    Posts:
    13
    Joined:
    Jul 18, 2010

    Jul 18, 2010
    13
    3
    16
    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 "";
    }
    }
     

Share This Page

Loading...