从android中的html内容中提取td和title标签?

时间:2017-09-05 11:43:24

标签: java android html

我在这个字符串变量内容中有html内容。我想从这个html内容字符串中提取标题标签。为获取此内容,我正在使用方法status()。使用httpclient。

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();

    }

所以现在,我想从这个html内容中提取标题标签,我正在使用这种方法,但我不能

$product = wc_get_product( $product_id );
$price = $product->get_price();

所以PLZ帮助我如何提取标题标签?

1 个答案:

答案 0 :(得分: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();
}

试试这个