-1

I have html content in string variable content like this.I want to extract title tag from this html content string. for get this content i am using the method status() as per under. using httpclient.

String content="<html>
<head>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"> 
<META HTTP-EQUIV="Refresh" CONTENT="300">
<title>Relay Control  - Cabin + Conference Logger</title>
</head>
<tr>
<td valign=top width="17%" height="100%">
<table width="100%" height="100%" align=center border=0 cellspacing=1 cellpadding=0>
    <tr><td valign=top bgcolor="#F4F4F4">
    <table width="100%" cellpadding=1 cellspacing=5>

    <tr><td align=center>

    <table><tr><td><a href="http://www.digital-loggers.com/1P.html"><img src="logo.gif" width=195 height=65 border=0 alt="Digital Loggers, Inc."></a></td>

    <td><b><font size=-1>Ethernet Power Controller</font></b></td></tr></table>
    <hr>
    </td></tr>



<tr><td nowrap><b><a href="/index.htm">Relay Control</a></b></td></tr>
<tr><td nowrap><b><a href="/admin.htm">Setup</a></b></td></tr>
<tr><td nowrap><b><a href="/script.htm">Scripting</a></b></td></tr>


<tr><td nowrap><b><a href="/rtc.htm">Date/Time</a></b></td></tr>
<tr><td nowrap><b><a href="/serial.htm">Serial Ports</a></b></td></tr>

<tr><td nowrap><b><a href="/ap.htm">AutoPing</a></b></td></tr>
<tr><td nowrap><b><a href="/syslog.htm">System Log</a></b></td></tr>
<tr><td nowrap><b><a href="/logout">Logout</a></b></td></tr>
<tr><td nowrap><b><a href="/support.htm">Support</a></b></td></tr>
<tr><td nowrap><b><a href="/help/">Help</a></b></td></tr>




</body>
</html>
";

so now, i want to extract title tag from this html content and i am using this method but i cant get

public static String status() {


        StringBuffer stringBuffer = new StringBuffer("");
        BufferedReader bufferedReader = null;
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet();

            URI uri = new URI("http://10.1.1.82/index.htm");
            httpGet.setURI(uri);
            httpGet.addHeader(BasicScheme.authenticate(
                    new UsernamePasswordCredentials("admin", "kirti123"),
                    HTTP.UTF_8, false));

            HttpResponse httpResponse = httpClient.execute(httpGet);

            HttpEntity entity = httpResponse.getEntity();
            Log.e("entity: ", "> " + entity);

            // Read the contents of an entity and return it as a String.
            content = EntityUtils.toString(entity);

            Log.e("content: ", "> " + content);


            //    String result = httpResponse.toString();
           htmlDocument = Jsoup.connect(content).get();
          htmlContentInStringFormat = htmlDocument.title();
            Log.e("title: ", "> " + htmlContentInStringFormat);

            InputStream inputStream = httpResponse.getEntity().getContent();
            bufferedReader = new BufferedReader(new InputStreamReader(
                    inputStream));

            String readLine = bufferedReader.readLine();
            while (readLine != null) {
                stringBuffer.append(readLine);
                stringBuffer.append("\n");
                readLine = bufferedReader.readLine();
            }
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    // TODO: handle exception
                }
            }
        }
        return stringBuffer.toString();

    }

so plz help me how i can extract title tag?

Devyani Kotadiya
  • 450
  • 5
  • 19

1 Answers1

0
public String[] GetTags(String html, String tagName) {

    List<String> result = new ArrayList<String>();

    String tagStart = "<" + tagName + ">";
    String tagEnd = "</" + tagName + ">";
    String tag_data;
    int end_index = 0;
    int last_index = 0;
    int start_index = 0;
    do {

        start_index = html.indexOf(tagStart,last_index+1);
        end_index = html.indexOf(tagEnd,last_index+1);
        last_index = end_index;
        if(end_index > 0) {
            tag_data = html.substring(start_index + tagStart.length(),end_index);
            result.add(tag_data);
        }
        else {
            break;
        }

    }while(true);

    return (String[]) result.toArray();
}

Try this